Java - 替换目录中的文件内容

时间:2016-06-16 17:22:22

标签: java file file-writing file-read java.nio.file

所以,最近我的任务是用另一个短语替换一系列文件中的重复短语。我期待一个单一的文件,我可以梳理它,但它是一堆巨大的目录,里面有大量的这些文件。

我的开发人员大脑开始了,所以我通过replaceAll()函数,我创建了这个小方法来查找目录中的所有文件及其所有子目录,并且它工作正常。

private static ArrayList<File> getAllFilesInDirectory(File directory) {
    ArrayList<File> filesInDirectory = new ArrayList<File>();

    if(!directory.isDirectory()) {
        filesInDirectory.add(directory);
        return filesInDirectory;
    } else {
        for(File fileInDirectory : directory.listFiles()) {
            if(!fileInDirectory.isDirectory())
                filesInDirectory.add(fileInDirectory);
            else
                filesInDirectory.addAll(getAllFilesInDirectory(fileInDirectory));
        }
        return filesInDirectory;
    }
}

因此,经过一些工作,我开发了一个程序,用另一个短语替换目录中的所有短语:

package xyz.ammartarajia.programs.rasid;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class RASID {
    public static void main(String[] args) {
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File("."));
        chooser.setDialogTitle("Open Directory...");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);
        chooser.showOpenDialog(null);

        String toReplace = JOptionPane.showInputDialog(null, "Please enter what you'd like to replace.", "To Replace", JOptionPane.QUESTION_MESSAGE),
                replaceWith = JOptionPane.showInputDialog(null, "Please enter what you'd like to replace the previous string with.", "Replace With", JOptionPane.QUESTION_MESSAGE);

        ArrayList<File> files = getAllFilesInDirectory(chooser.getSelectedFile());

        for(File file : files) {
            try(BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
                String newString = new String(Files.readAllBytes(Paths.get(file.getPath())), Charset.defaultCharset()).replaceAll(toReplace, replaceWith);
                System.out.println("New String: " + newString);
                writer.write(newString);
                writer.close();
            } catch(IOException e) {
                JOptionPane.showMessageDialog(null, "An error ocurred whilst editing the file!", "Error", JOptionPane.ERROR_MESSAGE);
                e.printStackTrace();
            }
        }
    }

    private static ArrayList<File> getAllFilesInDirectory(File directory) {
        ArrayList<File> filesInDirectory = new ArrayList<File>();

        if(!directory.isDirectory()) {
            filesInDirectory.add(directory);
            return filesInDirectory;
        } else {
            for(File fileInDirectory : directory.listFiles()) {
                if(!fileInDirectory.isDirectory())
                    filesInDirectory.add(fileInDirectory);
                else
                    filesInDirectory.addAll(getAllFilesInDirectory(fileInDirectory));
            }
            return filesInDirectory;
        }
    }
}

该程序存在的问题是,当文件以String的形式读入文件时,它会决定失败,而是给我一个空的String。我不确定为什么,但我认为它与路径有关。

修改 我已经更改了代码,使用readAllLines(Path, Charset)方法似乎没有任何区别。这是新代码:

package xyz.ammartarajia.programs.rasid;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class RASID {
    public static void main(String[] args) {
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File("."));
        chooser.setDialogTitle("Open Directory...");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);
        chooser.showOpenDialog(null);

        String toReplace = JOptionPane.showInputDialog(null, "Please enter what you'd like to replace.", "To Replace", JOptionPane.QUESTION_MESSAGE),
                replaceWith = JOptionPane.showInputDialog(null, "Please enter what you'd like to replace the previous string with.", "Replace With", JOptionPane.QUESTION_MESSAGE);

        ArrayList<File> files = getAllFilesInDirectory(chooser.getSelectedFile());

        for(File file : files) {
            try(BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
                String newString = new String(combineLines(Files.readAllLines(Paths.get(file.getPath()), Charset.defaultCharset()))).replaceAll(toReplace, replaceWith);
                System.out.println("New String: " + newString);
                writer.write(newString);
                writer.close();
            } catch(IOException e) {
                JOptionPane.showMessageDialog(null, "An error ocurred whilst editing the file!", "Error", JOptionPane.ERROR_MESSAGE);
                e.printStackTrace();
            }
        }
    }

    private static String combineLines(List<String> lines) {
        String linesAsString = "";
        for(String line : lines)
            linesAsString += line + '\n';
        return linesAsString;
    }

    private static ArrayList<File> getAllFilesInDirectory(File directory) {
        ArrayList<File> filesInDirectory = new ArrayList<File>();

        if(!directory.isDirectory()) {
            filesInDirectory.add(directory);
            return filesInDirectory;
        } else {
            for(File fileInDirectory : directory.listFiles()) {
                if(!fileInDirectory.isDirectory())
                    filesInDirectory.add(fileInDirectory);
                else
                    filesInDirectory.addAll(getAllFilesInDirectory(fileInDirectory));
            }
            return filesInDirectory;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您不应该以字节形式读取文件来进行文本操作。尝试Files.readAllLines()并迭代每个文件的行。

您的程序似乎正在读取非文本文件。从String(bytes [],Charset)的文档:

  

此方法始终使用此charset的默认替换字符串替换格式错误的输入和不可映射的字符序列。当需要更多地控制解码过程时,应该使用CharsetDecoder类。

请注意,使用此策略也会修改非文本文件中的“匹配”。

答案 1 :(得分:0)

正如@Daniel O在他的回答评论中所建议的那样,使用java.util.Scanner类似乎已经奏效了!

他的话:至少在你用这个问题发布的代码中,你使用一个非常复杂的表达来读取文件。您是否尝试跨多行打破该表达式,沿途打印中间值,以便您可以看到哪个特定部分失败?如果所有其他方法都失败了,您可以用另一种方式阅读文件,例如使用java.util.Scanner