import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class JavaCopyFile {
public static void main(String[] args) throws InterruptedException, IOException {
int i=0,count=0;;
while(i<15) {
File source = new File("error.txt");
File dest = new File("criteria.txt");
// copy file conventional way using Stream
//long start = System.nanoTime();
copyFileUsingStream(source, dest);
//System.out.println("Time taken by Stream Copy = " + (System.nanoTime() - start));
if(i<15) {
count++;
}
i++;
}
System.out.println(count);
}
private static void copyFileUsingStream(File source, File dest)
throws IOException {
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(source);
output = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
}catch(Exception e) {
System.out.println("File not found exception");/*finally {
input.close();
output.close();
*/
}
}
}
I have written code as above - for testing purpose I set count variable. count
is giving 15 it is perfect. But file is copying once only. I want to copy the file for 15 times to the same destination file. Please help me to solve this problem. I am a beginner in java programming.