如何将字符串写入到记事本中,而不覆盖原来的内容

public static void main(String[] args) {
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String date2=sdf2.format(date);
		String fileName=sdf.format(date);
		String path="E:\\log\\"+fileName+".txt";
		File file=new File(path);
		if(!file.exists()){
			try {
				FileOutputStream out=new FileOutputStream(file,true);
				out.write("hao".getBytes());
				out.write(",".getBytes());
				out.write("192.168.1.106,".getBytes());
				out.write(date2.getBytes());
				out.write("\n".getBytes());
				out.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else{
			try {
				RandomAccessFile raf=new RandomAccessFile(path,"rw");
				raf.seek(raf.length());
				raf.write("lin,192.168.1.200,".getBytes());
				raf.write(date2.getBytes());
				raf.write("\n".getBytes());
				raf.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}

 

以上代码是每天根据年月日生产一个记事本文件,用年月日来做为记事本的名字。如果根据当天的年月日命名的记事本不存在,那么就自动创建记事本。否则的话。就向该记事本中写入你想写入的信息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值