I want to compare all file names from a given directory
Input/Output code:
static Scanner sc = new Scanner(System.in);
static String Directory = sc.next();
static File folder = new File(Directory);
static File[]listofFiles = folder.listFiles();
static String[] underFiles = folder.list();
public static void main(String[] args) throws IOException {
Main main = new Main();
main.walk(Directory);
}
public void walk( String path ) {
File root = new File( path );
File[] list = root.listFiles();
if (list == null) {
return;
}
for (File f : list) {
if ( f.isDirectory() ) {
walk( f.getAbsolutePath() );
System.out.println( "Dir:" + f.getAbsoluteFile() );
}
else {
System.out.println( "File:" + f.getName() );
}
}
}
The input is to give a directory path. The output will show all of the files in the given directory. How can I compare equal file names in this directory?
Try this:
public static void main(String[] args) throws IOException {
String path = "/your/file-path/here";
try (Stream<Path> paths = Files.walk(Paths.get(path))) {
Map<String, List<Path>> map =
paths.filter(Files::isRegularFile)
.collect(groupingBy(p -> p.getFileName().toString()));
System.out.println(map);
}
}
Files.walk
will walk all the hierarchy of files for you. The only thing you need to do is to consume the Path
s from Stream
.
If you still want to stick with your own code. Then you might do this instead:
public static void walk(String path, Map<String, List<String>> map) {
File root = new File(path);
File[] list = root.listFiles();
if (list == null) return;
for (File f : list) {
if (f.isDirectory()) {
walk(f.getAbsolutePath(), map);
System.out.println("Dir:" + f.getAbsoluteFile());
} else {
map.computeIfAbsent(f.getName(), k -> new ArrayList())
.add(f.getAbsolutePath());
System.out.println("File:" + f.getName());
}
}
}
and then:
public static void main(String[] args) {
String fileName = "/your/file-path/here";
Map<String, List<String>> map = new HashMap<>();
walk(fileName, map);
System.out.println(map);
}
As I understand it, you want to locate directories that contain files with the same file name. For example, on a Windows machine, you may have several folders that contain image files. Usually each one of those folders will have a file named Thumbs.db . So you would like to know the names of the folders that contain a file named Thumbs.db .
The following code calls method walk of class java.nio.file.Files
and manipulates the Stream returned by that method to create a Map where the Map
key is the file name and the Map
value is a List of all the folders that contain a file with that name.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class WalkTree {
public static void main(String[] args) {
Path path = Paths.get(args[0]);
try (Stream<Path> paths = Files.walk(path)) {
Map<Path, List<Path>> map = paths.filter(p -> Files.isRegularFile(p))
.collect(Collectors.toMap(p -> p.getFileName(),
p -> new ArrayList<Path>(List.of(p.getParent())),
(l1, l2) -> {l1.addAll(l2); return l1;}));
}
catch (IOException xIo) {
xIo.printStackTrace();
}
}
}
The first parameter to the toMap
method, in the above code, is a Function that returns the Map
key. The second parameter is another Function
that returns the Map
value. And the third parameter is a BinaryOperator that merges two different Map
values that both have the same Map
key.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.