package com.JavaSE.day08;
import java.util.Date;
import java.util.Timer;
public class TimerTest {
public static void main(String[] args) {
Timer timer = new Timer();
MyTask mt1 = new MyTask("e:/Test/test1.txt", "hello");
//计时器从从现在开始执行(new Date()),每过1秒执行一次mt1里面的run方法
timer.schedule(mt1, new Date(), 1000);
for(int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mt1.setContext("" + i);
}
//终止计时器
timer.cancel();
}
}
package com.JavaSE.day08;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.TimerTask;
public class MyTask extends TimerTask {
/**
* pathName:文件路径及文件名
* context: 要写入文件的内容
*/
private String pathName;
private String context;
public MyTask(String pathName, String context) {
this.pathName = pathName;
this.context = context;
}
public boolean cancel() {
return super.cancel();
}
@Override
public void run() {
OutputStream os = null;
try {
os = new FileOutputStream(pathName, true);
os.write(context.getBytes());
os.write(("\r\n").getBytes());
} catch (FileNotFoundException e) {
System.out.println("文件不存在!");
} catch (IOException e) {
System.out.println("文件写入失败!");
}
try {
os.close();
} catch (IOException e) {
System.out.println("文件流关闭失败!");
}
}
public String getPathName() {
return pathName;
}
public void setPathName(String pathName) {
this.pathName = pathName;
}
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
最后补上将io流写成方法操作的
package com.HomeWork.day07;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FileOperation {
public String read(String filePathName) {
StringBuffer sb = new StringBuffer();
InputStream is = null;
try {
is = new FileInputStream(filePathName);
byte[] buffer = new byte[1024];
int len = 0;
while((len = is.read(buffer)) != -1) {
String s = new String(buffer, 0, len);
sb.append(s);
}
} catch (FileNotFoundException e) {
System.out.println("文件找不到!");
} catch (IOException e) {
System.out.println("文件读取失败!");
}
try {
is.close();
} catch (IOException e) {
System.out.println("文件读取流关闭失败!");
}
return sb.toString();
}
public void write(String filePathName, String context) {
File f = new File(filePathName);
if(!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
System.out.println("文件创建失败!");
}
}
OutputStream os = null;
try {
os = new FileOutputStream(filePathName, true);
os.write(context.getBytes());
os.write(("\r\n").getBytes()); //当把一个文件读取完毕之后,多打一个换行,以便下次复制文件可以显示在下一行
} catch (FileNotFoundException e) {
System.out.println("文件找不到!");
} catch (IOException e) {
System.out.println("文件写入失败");
}
try {
os.close();
} catch (IOException e) {
System.out.println("文件写入流关闭失败");
}
}
}
几个使用到的类Timer,TimerTask,Thread,接口:Runnable,锁关键字:synchronized