学生类
package expriment;
public class Student5 implement s java.io.Serializable{
private String name;
private int age;
public Student5() {
}
public Student5(String name, int age) {
this.name = name;
this.age = age;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Student5{name = " + name + ", age = " + age + "}";
}
}
写入和读取类还有序列化反序列化
package expriment;
import java.io.*;
//字符读取和写入类
class WriterAndRead {
//写入方法
public void w() {
Student5 s = new Student5("张三", 20);
Student5 s2 = new Student5("李四", 21);
String filePath = "D:\\JDK21\\untitled1\\src\\expriment\\a.txt";
//判断文件是否存在
if (!new File(filePath).exists()) {
System.out.println("文件不存在,所以不可写");
return;
}
try (FileWriter writer = new FileWriter(filePath)) {
writer.write(s.getName() + " " + s.getAge() + "\n" + s2.getName() + " " + s2.getAge());
writer.write("\n"+"掌握Java IO流基础:通过实际操作,深入理解Java IO流的概念、分类及工作原理," +
"为后续的文件读写操作打下坚实基础。\n"
+
"\n" +
"实现文本文件读写:利用FileWriter和FileReader类,学习如何将字符串数据写入文本文件," +
"并从文本文件中读取数据。此过程将加深对字符流操作的理解,并提升处理文本文件的能力。");
System.out.println("文本已成功写入文件:" + "a.txt");
} catch (IOException e) {
e.printStackTrace();
}
}
//读取方法
public void r() {
if (!new File("D:\\JDK21\\untitled1\\src\\expriment\\a.txt").exists()) {
System.out.println("文件不存在,所以不可读");
return;
}
try (FileReader reader = new FileReader("D:\\JDK21\\untitled1\\src\\expriment\\a.txt")) {
int c;
//计算读取时间
long t1 = System.currentTimeMillis();
while ((c = reader.read()) != -1) {
System.out.print((char) c);
}
System.out.println();
System.out.println("文本已成功读取:" + "a.txt");
System.out.println("读取时间:" + (System.currentTimeMillis() - t1) + "ms");
} catch (IOException e) {
e.printStackTrace();
}
//整行读取
try (BufferedReader br = new BufferedReader(new FileReader("D:\\JDK21\\untitled1\\src\\expriment\\a.txt"))) {
String line;
System.out.println("整行读取:");
//计算读取时间
long t2 = System.currentTimeMillis();
while ((line = br.readLine()) != null) {
System.out.println(line);
}
System.out.println("读取时间:" + (System.currentTimeMillis() - t2) + "ms");
} catch (IOException e) {
e.printStackTrace();
}
}
}
//序列化和反序列化
class ObjectSerializer1 {
//序列化方法
public void Serializerout() {
if (!new File("D:\\JDK21\\untitled1\\src\\expriment\\a1.txt").exists()) {
System.out.println("文件不存在,所以不可写");
return;
}
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\JDK21\\untitled1\\src\\expriment\\a1.txt"))) {
oos.writeObject(new Student5("张三", 20));
oos.writeObject(new Student5("李四", 21));
System.out.println("序列化成功");
} catch (IOException e) {
e.printStackTrace();
}
}
//反序列化方法
public void Deserializerin() {
if (!new File("D:\\JDK21\\untitled1\\src\\expriment\\a1.txt").exists()) {
System.out.println("文件不存在,所以不可读");
return;
}
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\JDK21\\untitled1\\src\\expriment\\a1.txt"))) {
Student5 s1 = (Student5) ois.readObject();
Student5 s2 = (Student5) ois.readObject();
System.out.println("反序列化成功");
System.out.println(s1);
System.out.println(s2);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
public class IOStreamExperiiment5 {
public static void main(String[] args) {
WriterAndRead wr = new WriterAndRead();
wr.w();
wr.r();
ObjectSerializer1 os = new ObjectSerializer1();
os.Serializerout();
os.Deserializerin();
}
}
大家一起来见证结果
