java旅行第二站--JavaSE第八天--用计时器将内容写入文件

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值