首页>代码>java IO工具类大全>/IO/src/IO/FileRW.java
package IO;

import java.io.*;
import java.util.*;

public class FileRW {
	// ��Ҫ�����person��Ŀ�� 
	public static int NUMBER = 3;

	public static void main(String[] args) {
		Person[] people = new Person[NUMBER];
		// ��ʱ����������ݵ���ʱ�ַ����顣 
		String[] field = new String[4];
		// ��ʼ��field���顣 
		for (int i = 0; i < 4; i++) {
			field[i] = "";
		}
		// IO�������벶��IO�쳣�� 
		try {
			// ���ڶ�field����������ӿ��ơ� 
			int fieldcount = 0;
			// ��ʹ��System.in����InputStreamReader���ٹ���BufferedReader�� 
			BufferedReader stdin = new BufferedReader(new InputStreamReader(
					System.in));
			for (int i = 0; i < NUMBER; i++) {
				fieldcount = 0;
				System.out.println("The number " + (i + 1) + " person");
				System.out
						.println("Enter name,age,salary,married(optional),please separate fields by ':'");
				// ��ȡһ�С� 
				String personstr = stdin.readLine();
				// ���÷ָ��� 
				StringTokenizer st = new StringTokenizer(personstr, ":");
				// �ж��Ƿ��зָ�����á� 
				while (st.hasMoreTokens()) {
					field[fieldcount] = st.nextToken();
					fieldcount++;
				}
				// �������married����field[3]��Ϊ�գ����þ����ĸ������Person���캯�� 
				if (field[3] != "") {
					people[i] = new Person(field[0],
							Integer.parseInt(field[1]), Double
									.parseDouble(field[2]), field[3]);
					// ��field[3]Ϊ�գ��Ա��´�����ʹ�á� 
					field[3] = "";
				}
				// ���δ����married����field[3]Ϊ�գ����þ�����������Person���캯�� 
				else {
					people[i] = new Person(field[0],
							Integer.parseInt(field[1]), Double
									.parseDouble(field[2]));
				}
			}
			// ���������ݱ�������people.dat���ı��ļ��С� 
			PrintWriter out = new PrintWriter(new BufferedWriter(
					new FileWriter("people.dat")));
			writeData(people, out);
			// �ر����� 
			out.close();
			// ���ļ���people.dat����ȡ��ݡ� 
			BufferedReader in = new BufferedReader(new FileReader("people.dat"));
			Person[] inPeople = readData(in);
			// �ر����� 
			in.close();
			// ������ļ��ж������ݡ� 
			for (int i = 0; i < inPeople.length; i++) {
				System.out.println(inPeople[i]);
			}
		} catch (IOException exception) {
			System.err.println("IOException");
		}
	}

	// ����������������� 
	static void writeData(Person[] p, PrintWriter out) throws IOException { 
      // д���¼�������� 
      out.println(p.length); 
      for (int i=0  ;i< p.length; i++) { 
      p[i].writeData(out); 
      } 
      }
	// ���������������?
	static Person[] readData(BufferedReader in) throws IOException {
		// ��ȡ��¼�������� 
		int n = Integer.parseInt(in.readLine());
		Person[] p = new Person[n];
		for (int i = 0; i < n; i++) {
			p[i] = new Person();
			p[i].readData(in);
		}
		return p;
	}
}

class Person {
	private String name;
	private int age;
	private double salary;
	private String married;

	public Person() {
	}

	public Person(String n, int a, double s) {
		name = n;
		age = a;
		salary = s;
		married = "F";
	}

	public Person(String n, int a, double s, String m) {
		name = n;
		age = a;
		salary = s;
		married = m;
	}

	public String getName() {
		return name;
	}

	public int getAge() {
		return age;
	}

	public double getSalary() {
		return salary;
	}

	public String getMarried() {
		return married;
	}

	// ���������ʽ�� 
	public String toString() {
		return getClass().getName() + "[name=" + name + ",age=" + age
				+ ",salary=" + salary + ",married=" + married + "]";
	}

	// д��һ����¼����һ���˵���ݵ�������� 
	public void writeData(PrintWriter out) throws IOException {
		// ��ʽ����?
		out.println(name + ":" + age + ":" + salary + ":" + married);
	}

	// ������������һ����¼����һ���˵���ݡ� 
	public void readData(BufferedReader in) throws IOException {
		String s = in.readLine();
		StringTokenizer t = new StringTokenizer(s, ":");
		name = t.nextToken();
		age = Integer.parseInt(t.nextToken());
		salary = Double.parseDouble(t.nextToken());
		married = t.nextToken();
	}
}
最近下载更多
1358849392  LV21 2022年11月11日
A_xiaobao  LV9 2021年7月12日
CxlyboSoft  LV6 2020年2月27日
zhushizhan  LV3 2019年12月16日
故事_sun  LV26 2018年5月25日
liuyouminglove  LV2 2018年5月5日
diligentcat  LV2 2016年11月7日
Yuancc  LV21 2016年7月29日
developerAndroid  LV1 2016年7月26日
likoaong  LV11 2016年5月27日
最近浏览更多
1358849392  LV21 2022年11月11日
crosa_Don  LV18 2022年7月2日
双鱼座程序员7号  LV6 2022年4月23日
You're'ere I live. 2021年10月15日
暂无贡献等级
A_xiaobao  LV9 2021年7月12日
ahdaudha  LV7 2021年4月9日
1342203642  LV10 2020年9月1日
linjh123  LV1 2020年7月2日
Gyq灬ming  LV11 2020年6月22日
nhslailuo  LV2 2020年5月14日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友