Java04 save and read the .txt
来源:互联网 发布:als算法 与svd 编辑:程序博客网 时间:2024/04/30 10:15
Now there have a class named Person:
public class Person {
public static int lastId = 0;
private String name;
private Date dateOfBirth;
private String address;
public Person(String name, Date dateOfBirth, String address) {
super();//Now I don’t what it’s.When I search it, I only know that super is the same as parentclass.Using super() means that subclass can invoke all the parentclass’s methods.
this.name = name;
this.dateOfBirth = dateOfBirth;
this.address = address;
lastId++;
this.idNumber = lastId;
}
public String toString() {
return "Person [name=" + name + ", dateOfBirth=" + dateOfBirth
+ ", address=" + address + ", idNumber=" + idNumber + ",phoneNum="+phoneNum+"]";
}
public String flatten() {
return name + "," + dateOfBirth.getTime() + "," + address + "," +phoneNum+","+ idNumber;
}
}
Store the Person’s message:
public static void storeObjects(Person[] arr) {
File file = new File("PersonFile.txt");
try {
BufferedWriter fw = new BufferedWriter(new FileWriter(file, true));
for (int i = 0; i < arr.length; i++) {
fw.write(arr[i].flatten());//To invoke Person. flatten() to stroe message by some rules.
fw.newLine();
}
fw.flush();
fw.close();
} catch (IOException e) {
}
}
Read the .txt’s messages
public static boolean readObjects(Person[] arr) {
File file = new File("PersonFile.txt");
String line;
Person x;
String name;
Date dob;
String address;
int id;
int position = 0;
try {
BufferedReader fr = new BufferedReader(new FileReader(file));
while ((line = fr.readLine()) != null) {
String[] s = line.split(",");
// The message we stroe are liking that:
//We using a (String line) to receive one line message,then invode line. split(",") to get a String[].
name = s[0];
dob = new Date(Long.parseLong(s[1]));
address = s[2];
id = Integer.parseInt(s[3]);
x = new Person(name, dob, address);
arr[position] = x;
position++;
}
fr.close();
} catch (IOException e) {
return false;
}
return true;
}
- Java04 save and read the .txt
- android save and read webpage
- cocoa read xml and save xml
- JAVA04
- What is the difference between --save and --save-dev?
- Save and read double array in a binary file
- Windows CE: Save and Restore the Registry
- How to read and save Images in a Sql Server Database using ADO.NET and C#
- iptables-save(txt)
- Excute shell and read the output
- Understanding save() and restore() for the Canvas Context
- x86 function call and return --- stack save the return address.
- Read the book - Self Leadership and the One Minute Manager
- save("vara.txt",[a]) 、load("vara.txt")
- read txt,ini
- J2ME read txt file
- Matlab Read Txt 文件
- read .txt file
- Oracle 字符集的查看和修改
- Oracle中的character set 和 national character set
- Java菜鸟学习笔记--配置篇(三):Java API 文档下载与文档注释的使用
- 基數排序法
- WCF配置故障
- Java04 save and read the .txt
- ABAP查询Table方法
- TUN/TAP
- 将dataset中的数据导出至Excel中而不需要安装MS Excel的方法(含UNICODE支持)
- 汽水瓶
- bebug assertion failed问题解决
- linux下实现虚拟网卡TAP/TUN例子
- Java05 Using Gui
- mysql笔记(二)