【问题标题】:print text show the file path using Java使用 Java 打印文本显示文件路径
【发布时间】:2016-09-30 11:57:49
【问题描述】:

我需要一个java方法来检查文本

【问题讨论】:

  • 到目前为止你尝试过什么?
  • 已经有工具可以做到这一点。通常使用 shell 脚本,但您可以从 Java 中调用它们。

标签: java algorithm checksum fileutils apache-stringutils


【解决方案1】:

您可以逐个文件比较大小,将大小相同的文件保存到数组列表中,然后打印此数组列表信息。

是一种非常简单粗暴的方法,如果你有一个包含很多链接的文件夹或类似的东西可能无法正常工作。

如果你想进一步调查,你可以使用校验和来检查,但是你需要这方面的知识。

【讨论】:

    【解决方案2】:

    如果你想在 Java 中做到这一点,你可以使用 commons.io Apache 库,使用方法 iterateFiles 获取目录中的文件,使用方法 contentEquals 检查两个文件是否具有相同的内容。

    进口

    import org.apache.commons.io.FileUtils;
    

    代码

    final String path = "C:/...";
    List<File> files = new ArrayList<>();
    Iterator iterator = FileUtils.iterateFiles(new File(path), null, false);
    while(iterator.hasNext()){
        // Compare with the rest of the files in the array
        final File file = iterator.next();
        for (int i = 0; i < files.size(); i++) {
            if (FileUtils.contentEquals(file, files.get(i))) {
                // Here you can show the file path name
            }
        }
    
        // Add the file to the array
        files.add(file);
    }
    

    【讨论】:

    • 感谢维克多的精彩回答。我是Java初学者,请你写下剩下的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多