public class FileWriteThread implements Runnable {
static boolean isDestroy = false;
static int type;
private long times = 0;
public FileWriteThread() {
}
public void run() {
while (!isDestroy) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
switch (type) {
case 1:
writeFile1();
break;
case 2:
writeFile2();
break;
default:
System.out.println("Err write type");
}
System.out.println("Write times = " + times);
}
}
private void writeFile1() {
File f = new File("a.txt");
BufferedWriter bw = null;
FileWriter fw = null;
try {
fw = new FileWriter(f);
bw = new BufferedWriter(fw);
bw.write(""+times);
bw.newLine();
bw.flush();
for (int i = 0; i < 1000; i++) {
bw.write("index="+times);
bw.newLine();
bw.flush();
}
} catch (Exception ex) {
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException ex1) {
}
}
}
times++;
}