[英]How to compare all file names from a given directory java?
我想比较给定目录中的所有文件名
输入/输出代码:
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() );
}
}
}
输入是给出一个目录路径。 output将显示给定目录中的所有文件。 如何比较此目录中相同的文件名?
尝试这个:
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
将为您遍历文件的所有层次结构。 您唯一需要做的就是使用Stream
中的Path
。
如果您仍然想坚持使用自己的代码。 然后你可以这样做:
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());
}
}
}
接着:
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);
}
据我了解,您希望找到包含具有相同文件名的文件的目录。 例如,在 Windows 机器上,您可能有多个包含图像文件的文件夹。 通常这些文件夹中的每一个都有一个名为Thumbs.db的文件。 因此,您想知道包含名为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用那个名字。
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();
}
}
}
在上面的代码中, toMap
方法的第一个参数是一个Function ,它返回Map
键。 第二个参数是另一个Function
,它返回Map
值。 第三个参数是BinaryOperator ,它合并了两个不同的Map
值,它们都具有相同的Map
键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.