java - 将文本添加到具有条件的目录中的多个文件 (Java)

标签 java directory filewriter file-read

我有一个目录,其中包含一堆子目录,每个子目录包含多个文本文件。每个子目录都包含一个文本文件,该文件与包含它的子目录同名;我想将此文件的内容添加到所有其他文本文件中,除了与子目录共享名称的文件,并且我想用 java 制作它。

例如,给定目录“a”,包含子目录“b”、“c”、“d”,分别包含文件“b”、“e”、“f”和“c”、“g”、“h”和“d”、“i”、“j”,我希望目录“a”包含子目录“b”、“c”、“d”,分别包含文件“b”、“b+e”、“b+f”和“c”、“c+” g”、“c+h”和“d”、“d+i”、“d+j”,并可能删除添加的文件,即文件“a”、“b”、“c”。

final File folder = new File("/path-of-directory/");

public static void listFilesForFolder(final File folder) {
    for (final File fileEntry : folder.listFiles()) {
        if (fileEntry.isDirectory()) {
            listFilesForFolder(fileEntry);
        } else if (fileEntry.getName().substring(0, fileEntry.getName().lastIndexOf(".")).toLowerCase().equals(folder.getName().toLowerCase()))
        
               // found the file which content has to be added to other files in the same directory. How to?
        }
}

最佳答案

这个解决方案对我来说效果很好,请务必阅读评论以了解详细信息:

public class FileCopy {

    public static void recursivelyCopyContent(final File folder) {
        for (final File fileEntry : folder.listFiles()) {
            if (fileEntry.isDirectory()) {
                recursivelyCopyContent(fileEntry);
            } else if (fileEntry.getName().substring(0, fileEntry.getName().lastIndexOf(".")).toLowerCase().equals(folder.getName().toLowerCase())) {
                // just pass the folder and the file to the method that will do the actual content copy 
                copyContent(folder, fileEntry);
            }
        }
    }

    public static void copyContent(final File folder, final File fileName) {
        try {
             // get the byte[] content of the source file
            byte[] fileContent = Files.readAllBytes(fileName.toPath());
            for (final File fileEntry : folder.listFiles()) {
                // only select files with a different absolute path than the source one
                if(fileEntry.isFile() && !fileEntry.getAbsolutePath().equals(fileName.getAbsolutePath())) {
                    // append contents from source to destination file 
                    Files.write(fileEntry.toPath(), fileContent, StandardOpenOption.APPEND);
                }
            }
        }catch (IOException e) {
            e.getStackTrace();
        }
    }

    public static void main(String[] args) {
        final File folder = new File("/path-of-directory/");
        FileCopy.recursivelyCopyContent(folder);
    }
}

只需注意不带扩展名的文件:您的情况

fileEntry.getName().substring(0, fileEntry.getName().lastIndexOf(".")).toLowerCase().equals(folder.getName().toLowerCase())

将抛出java.lang.StringIndexOutOfBoundsException,因为其名称中不存在.字符。

在这种情况下,我建议采用以下方法:

// both cases covered: files have or do not have an extension
if (
    (!fileEntry.getName().contains(".") && fileEntry.getName().toLowerCase().equals(folder.getName().toLowerCase())) ||
    (fileEntry.getName().contains(".") && fileEntry.getName().substring(0, fileEntry.getName().lastIndexOf(".")).toLowerCase().equals(folder.getName().toLowerCase())) 
)

关于java - 将文本添加到具有条件的目录中的多个文件 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43064000/

相关文章:

java - 在 JMH 应用程序中使用 Spring Bean

java - 使用链表添加多个项目,JAVA

java - 编译并执行主类,引用jar中的类文件

python - 根据文件名创建文件夹

java - 在类中存储 byte[]

powershell - Get-Childitem目录通配符 “Access is denied”

python - 递归测试文件夹是否完全为空的最佳方法?

java - java多线程是否可以优化多文件写入

java - 写入文件 : IOException: Stream closed

java - 如何在使用 ANTLR (使用 java)解析文件后保存文件?