【问题标题】:Java, copy from a file to another, line by line with an intervalJava,从一个文件复制到另一个,逐行间隔
【发布时间】:2017-07-30 15:15:08
【问题描述】:

我有一个包含 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个文件,我需要使用线程吗? 感谢您的帮助。

【问题讨论】:

  • 我没有得到你问题的第二部分 但就我而言,我在新文件中处理了 120 行,其中 0 行直到最后,然后我找到了我想要的数据我不是在找。
  • 对不起,我没有正确表达我想要的东西。使用此代码,我必须等待 120 秒才能看到文件中的数据,而我想要的是在 40 秒后找到新文件中的 40 行。
  • 对于第二部分,你也可以在一个线程中完成,但这会很混乱。对于一个干净的解决方案,您应该使用一个线程来处理一个文件。

标签: java file copy buffer line


【解决方案1】:

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

您可以在需要将数据保存在磁盘上时手动刷新缓冲区。在println() 之后调用flush() 如下。

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

【讨论】:

  • 我刚试过,它工作正常。谢谢!如果我想同时处理 4 个文件,我需要使用 Threads 吗?对吗?
  • @RahXenLegacy。 Thread 当你想并行处理多个东西时很有意义。
【解决方案2】:

你可以调用一个

pw.flush();

紧接着

pw.println(line);

这应该可以解决问题。

【讨论】:

  • 感谢您的帮助!
  • @RahXenLegacy 你知道你仍然可以投票(不接受)多个答案
  • 是的,我做到了,但我弹出了一个关于声誉低于 15 分的信息。
【解决方案3】:

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

    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;
    }
}

【讨论】:

  • 我尝试了输出到控制台的代码以跟踪该过程,但似乎无法访问方法 write
猜你喜欢
  • 2013-02-17
  • 2011-01-31
  • 1970-01-01
  • 2015-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-26
相关资源
最近更新 更多