package com;
import java.io.Serial;
import java.io.Serializable;
public class student implements Serializable {
private static final long serialVersionUID = -2997153067269042279L;
private int age;
private String name;
private String address;
public student() {
}
public student(int age, String name, String address) {
this.age = age;
this.name = name;
this.address = address;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return address
*/
public String getAddress() {
return address;
}
/**
* 设置
* @param address
*/
public void setAddress(String address) {
this.address = address;
}
public String toString() {
return "student{age = " + age + ", name = " + name + ", address = " + address + "}";
}
}
package com;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class demo {
public static void main(String[] args) throws IOException {
//创建对象
student s1=new student(23,"zs","北京");
student s2=new student(22,"ww","上海");
student s3=new student(21,"ls","广东");
//创建集合
ArrayList<student> list=new ArrayList<>();
list.add(s1);
list.add(s2);
list.add(s3);
//序列化对象
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("C:\\Users\\彭新晨\\IdeaProjects\\untitled6\\a.txt"));
oos.writeObject(list);
oos.close();
}
}
package com;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
public class demo1 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//反序列化对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("C:\\Users\\彭新晨\\IdeaProjects\\untitled6\\a.txt"));
//读取
ArrayList<student> list=(ArrayList<student>) ois.readObject();
//循环list
for (student student : list) {
System.out.println(student);
}
ois.close();
}
}
package com;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.nio.charset.Charset;
public class demo1 {
public static void main(String[] args) throws FileNotFoundException {
//创建字节打印流
PrintStream ps=new PrintStream(new FileOutputStream("C:\\Users\\彭新晨\\IdeaProjects\\untitled6\\a.txt"),true,Charset.forName("UTF-8"));
ps.println(88);
ps.print("流");
ps.println();
ps.printf("%s 爱上了 %s","啊真","啊强");
ps.close();
}
}
package com;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class demo1 {
public static void main(String[] args) throws IOException {
//创建字符打印流
PrintWriter pw=new PrintWriter(new FileWriter("C:\\Users\\彭新晨\\IdeaProjects\\untitled6\\a.txt",true));
pw.println("啦啦啦啦啦啦啦");
pw.print(88);
pw.printf("%s 爱上了 %s","啊真","啊强");
pw.close();
}
}