Java, copy from a file to another, line by line with an interval

Viewed 5806

I have a file with 120 lines and I want to move them one by one to another file with an interval for example of 1 seconds and to be able to find after 10 seconds 10 lines in the new file.

But for my case, I execute the program with 0 lines in the new files until the end, and then I find the data.

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

Second, for 4 files to process in the same moment with different interval, I need to use Threads ? Thanks for your help.

3 Answers
Related