簡體   English   中英

如何比較給定目錄 java 中的所有文件名?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM