javaIO流 IO缓冲流实现读写 简单易懂(3)

首先写一个bean类

public class Person {
	private String name;
	private String sex;
	private int age;
	private String marry;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getMarry() {
		return marry;
	}
	public void setMarry(String marry) {
		this.marry = marry;
	}
	public Person(String name, String sex, int age, String marry) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
		this.marry = marry;
	}
	public Person() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", sex=" + sex + ", age=" + age
				+ ", marry=" + marry + "]";
	}
	
	
	
}

然后再创建一个类

public class BuffDuXie {
	public static void main(String[] args) throws IOException {
		xie();
		du();
	}
	
//	写入
	public static void xie() throws IOException{
		FileWriter f = new FileWriter("D:\\c.txt",true);
		BufferedWriter bf = new BufferedWriter(f);
		bf.write("张三,男,18,未婚");
		bf.newLine();
		bf.close();
	}
	
	
//	读取
	public static void du() throws IOException{
		FileReader f = new FileReader("D:\\c.txt");
		BufferedReader bf = new BufferedReader(f);
		String str = "";
		while((str = bf.readLine()) != null){
			System.out.println(str);
			String[] s = str.split(",");
			int age = Integer.parseInt(s[2]);
			Person p = new Person(s[0],s[1],age,s[3]);
			System.out.println(p);
		}
		bf.close();
	}
	
}

打印结果

张三,男,18,未婚
Person [name=张三, sex=男, age=18, marry=未婚]

你可能感兴趣的:(笔记)