2017-07-30 119 views
3

我有一个120行的文件,我想将它们逐个移动到另一个文件,间隔为1秒,并且能够在10秒后找到新文件中的10行。Java从一个文件复制到另一个文件,一行一行,间隔

但对于我而言,我在新文件中用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个文件要在不同的时间间隔同时处理,我需要使用线程? 感谢您的帮助。

+0

我还不知道你问题的第二部分*但是,对于我的情况,我处理120行,新文件中有0行,直到结束,然后找到我不想查找的数据。* – Ravi

+0

对不起,我没有正确表达我想要的内容。有了这段代码,我必须等待120秒才能看到文件中的数据,而我想要的是在40秒后找到新文件中的40行。 –

+0

对于第二部分,您也可以在一个线程中完成,但这会很麻烦。对于干净的解决方案,您应该使用一个线程来处理一个文件。 –

回答

1

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

您可以手动刷新缓冲区,以便您需要将数据存入磁盘时。在println()之后,请拨打flush()

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

我刚试过,它正在工作。谢谢! 如果我想在同一时间处理4个文件,我需要使用线程?对 ? –

+0

@RahXenLegacy。当你想要并行处理多个东西时,“线程”是有意义的。 – Ravi

0

您可以调用后直接

pw.println(line); 

pw.flush(); 

这应该做的伎俩。

+0

感谢您的帮助! –

+0

@RahXenLegacy你知道你仍然可以upvote(不接受)多个答案<;-) –

+0

是的,我做到了,我有一个弹出低于15点的声誉。 –

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

我试着输出到控制台的代码来跟踪过程,但似乎没有访问方法写 –

相关问题