Java,从一个文件复制到另一个文件,一行一行

时间:2017-07-30 15:15:08

标签: java file copy buffer line

我有一个包含120行的文件,我希望将它们逐个移动到另一个文件,间隔为例如1秒,并且能够在10秒内找到新文件中的10行。

但是对于我的情况,我在新文件中用0行执行程序直到结束,然后我找到数据。

String sourceFileName = "D:\\oldfile.txt";
String destinationFileName = "D:\\newfile.txt";

if(evt.getSource() == btnProcess)
{
    BufferedReader br = null;
    PrintWriter pw = null; 
    try {
         br = new BufferedReader(new FileReader(sourceFileName));
         pw =  new PrintWriter(new FileWriter(destinationFileName));
         String line;
         while ((line = br.readLine()) != null) {
                pw.println(line);
                Thread.sleep(1000);
         }
         br.close();
         pw.close();
    }catch (Exception e) {
         e.printStackTrace();
    }
}

其次,对于4个文件在同一时刻处理不同的间隔,我需要使用Threads? 谢谢你的帮助。

3 个答案:

答案 0 :(得分:2)

当您写入文本文件时,PrintWriter不会立即将其写入磁盘。相反,它将数据保存在内存中的缓冲区中。

当您需要将数据放在磁盘上时,可以手动刷新缓冲区。在println()致电flush()之后,就在下面。

     while ((line = br.readLine()) != null) {
            pw.println(line);
            pw.flush();
            Thread.sleep(1000);
     }

答案 1 :(得分:0)

你可以打电话给

pw.flush();

直接

pw.println(line);

这应该可以解决问题。

答案 2 :(得分:0)

至于你的第二部分,如果你不想使用线程,你可以做这样的事情:

    public static void main(final String[] args) {
    FileCopyDto[] files = new FileCopyDto[] {
            new FileCopyDto("D:\\oldfile.txt", "D:\\newfile.txt", 5),
            new FileCopyDto("D:\\oldfile2.txt", "D:\\newfile2.txt", 1)
    };

    try {
        boolean dataAvailable = true;
        int secondCount = 0;
        while (dataAvailable) {
            dataAvailable = false;
            for (FileCopyDto d : files) {
                d.write(secondCount);
                dataAvailable = dataAvailable || d.isDataAvailable();
            }
            secondCount++;
            Thread.sleep(1000);
        }
        for (FileCopyDto d : files) {
            d.close();
        }

    }catch (Exception e) {
        e.printStackTrace();
    }
}

static class FileCopyDto {
    String sourceFileName;
    String destinationFileName;
    int timeInSeconds;
    BufferedReader br = null;
    PrintWriter pw = null;
    String nextLine;

    public FileCopyDto(final String sourceFileName,
            final String destinationFileName,
            final int timeInSeconds) {
        this.sourceFileName = sourceFileName;
        this.destinationFileName = destinationFileName;
        this.timeInSeconds = timeInSeconds;
    }

    public void open() throws IOException {
        br = new BufferedReader(new FileReader(sourceFileName));
        pw =  new PrintWriter(new FileWriter(destinationFileName));
    }

    public boolean isDataAvailable() throws IOException {
        if (br == null) {
            open();
        }
        return (nextLine == null) || ((nextLine = br.readLine()) != null);
    }

    public void write(final int secondCount) {
        if (nextLine != null && secondCount % timeInSeconds == 0) {
            pw.println(nextLine);
            pw.flush();
            nextLine = null;
        }
    }

    public void close() throws IOException {
        br.close();
        pw.close();
        br = null;
    }
}