2017-07-30 108 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

我試着輸出到控制檯的代碼來跟蹤過程,但似乎沒有訪問方法寫 –

相關問題