1.2.3 以文本格式存储对象
可使用特殊形式进行存储对象数据,比如json格式 {"a":"b","c":[1,2,3]},或者xml,<v><a>b<a><c><1><2><3><c><v>,或者自定义格式,如指定分割符,可考虑使用序列化相关的方法完成读入,写出方法的拦截,从而实现自定义的效果。
readObject:将数据写入流
writeObject:将数据从流读出
作者示例:把对象写入文件
相比原文略有改动,close()写在了finally
TestFileTest.java
❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class TestFileTest {
public static void main(String[] args) {
Employee[] staff = new Employee[3];
staff[0] = new Employee("Carl Cracker", 75000,1987, 12, 15);
staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);
PrintWriter out = null;
Scanner in = null;
try{
out = new PrintWriter("employee.dat");
writeData(staff, out);
in = new Scanner(new FileReader("employee.dat"));
Employee[] newStaff = readData(in);
for(Employee e:newStaff){
System.out.println(e);
}
}catch (IOException e){
e.printStackTrace();
}finally {
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
}
private static void writeData(Employee[] employees, PrintWriter out) throws IOException{
out.println(employees.length);
for(Employee e:employees){
e.writeData(out);
}
}
private static Employee[] readData(Scanner in){
int n = in.nextInt();
in.nextLine();
Employee[] employees = new Employee[n];
for(int i = 0; i < n; i++){
employees[i] = new Employee();
employees[i]. readData(in);
}
return employees;
}
}
❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤
Employee.java
❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;
public class Employee {
private String name;
private double salary;
private Date hireDay;
public Employee(){}
public Employee(String n, double s, int year, int month, int day){
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year,month-1, day);
hireDay = calendar.getTime();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Date getHireDay() {
return hireDay;
}
public void setHireDay(Date hireDay) {
this.hireDay = hireDay;
}
public void raiseSalary(double byPercent){
double raise = salary*byPercent/100;
salary+=raise;
}
@Override
public String toString() {
return getClass().getName() + "[name="+name+",salary="+salary+",hireDay="+hireDay+"]";
}
public void writeData(PrintWriter out){
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(hireDay);
out.println(name+"|"+salary+"|"+calendar.get(Calendar.YEAR)+"|"+(calendar.get(Calendar.MONTH)+1)+"|"+
calendar.get(Calendar.DAY_OF_MONTH));
}
public void readData(Scanner in){
String line = in.nextLine();
String[] tokens = line.split("\\|");
name = tokens[0];
salary = Double.parseDouble(tokens[1]);
int y = Integer.parseInt(tokens[2]);
int m = Integer.parseInt(tokens[3]);
int d = Integer.parseInt(tokens[4]);
GregorianCalendar calendar = new GregorianCalendar(y, m-1, d);
hireDay = calendar.getTime();
}
}
❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤
运行结果:
🚗🚓🚕🛺🚙🚌🚐🚎🚑🚒🚚🚛🚜🚘🚔🚖🚍🦽🦼🛹🚲🛴🛵🏍
🚗🚓🚕🛺🚙🚌🚐🚎🚑🚒🚚🚛🚜🚘🚔🚖🚍🦽🦼🛹🚲🛴🛵🏍
1.2.4 字符集
字符保存在文件中有各种格式,比较常用的有 ISO-8899-1,UTF-8,如果大概记得,不太确定是否有横线,可以查看 StandardCharsets 确定:
如果不在这里面(不常见),可以查看IDEA右下角的more:
来确定具体的名字有无横线,或者也可以用 Charset.isSupported(别名) 确定是否书写正确。输出所有系统字符,因为太多了,为了一屏能显示完,一行放100长度:
❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤
public class Main {
public static void main(String[] args){
Map<String,Charset> map = Charset.availableCharsets();
int len = 0;
for(String charsetName:map.keySet()){
len += charsetName.length();
if(len >= 100){
len = 0;
System.out.println();
len = charsetName.length()+1;
}
len += 1;
System.out.print(charsetName);
System.out.print(" ");
}
}
}
❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤
运行结果:
🚗🚓🚕🛺🚙🚌🚐🚎🚑🚒🚚🚛🚜🚘🚔🚖🚍🦽🦼🛹🚲🛴🛵🏍
🚗🚓🚕🛺🚙🚌🚐🚎🚑🚒🚚🚛🚜🚘🚔🚖🚍🦽🦼🛹🚲🛴🛵🏍
相关内容:选择 《Java核心技术 卷1》查找相关笔记
评论🌹点赞👍收藏✨关注👀,是送给作者最好的礼物,愿我们共同学习,一起进步
公众号 钰娘娘知识汇总