java IO字符流的使用记录

一个使用java 字节流的一个记录,虽然简单,但是自己写的就是~~~对吧,免得下次忘了又难得翻书。

package com.neo.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import com.neo.domain.Date;
import com.neo.service.DateCheck;
import com.neo.service.PreviousDate;

public class Test {
	public static void main(String[] args) {
		try {
			File date = new File("date.txt");
			File preDate = new File("predate.txt");
			if(!preDate.exists()){
				preDate.createNewFile();
			}
			FileReader fr = new FileReader(date);
			FileWriter fw = new FileWriter(preDate);
			BufferedReader br = new BufferedReader(fr);
			BufferedWriter bw = new BufferedWriter(fw);
			String hasRead;
			while(( hasRead = br.readLine()) != null ){
				Date today = new Date(hasRead);
				String message = DateCheck.dateCheck(today);
				if(message.contains("不合法")){
					bw.write("输入时间为: "+today);
					bw.write("该日期格式不正确!!!"+message+'\n');
					bw.flush();
					System.out.print("输入时间为: "+today);
					System.out.println("该日期格式不正确!!!"+message);
				}else{
					bw.write("输入时间为: "+today + " 前一天为: ");
					bw.write(PreviousDate.prevDate(today)+" "+ '\n');
					System.out.print("输入时间为: "+today + " 前一天为: ");
					System.out.println(PreviousDate.prevDate(today));
				}
			}
			br.close();
			fr.close();
			bw.close();
			fw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
 

其中要注意一点:java文件的读取,这里放在项目目录下,直接用File操作时能找到。

要是这样,


系统将会说  java.io.FileNotFoundException: date.txt (系统找不到指定的文件。)

下次再补充文件读取的知识吧,记录一把。




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值