java学习
来源:互联网 发布:水果合成软件下载 编辑:程序博客网 时间:2024/06/03 16:22
package po2;
import java.io.*;
public class Ioexercise2 {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
//准备数据
Student[] stuArray = new Student[]{new Student(1,"A",18,1),new Student(2,"B",19,2),new Student(3,"C",20,3)};
String fileName="myfile.txt"; //代表本工程根目录下的文件
//序列化——写出对象
for(int i=0;i<stuArray.length;i++)
{
writeStuObject(fileName,stuArray[i]);
Student stuArray1=(Student)readStuObject(fileName);
System.out.println(stuArray1);
}
}
private static Student readStuObject(String fileName) throws FileNotFoundException, IOException, ClassNotFoundException {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(fileName));
Student stuArray1=(Student)ois.readObject();
ois.close();
return stuArray1;
}
private static void writeStuObject(String fileName, Student stuArray) throws FileNotFoundException, IOException {
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(fileName));
oos.writeObject(stuArray);
oos.close();
}
}
import java.io.*;
public class Ioexercise2 {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
//准备数据
Student[] stuArray = new Student[]{new Student(1,"A",18,1),new Student(2,"B",19,2),new Student(3,"C",20,3)};
String fileName="myfile.txt"; //代表本工程根目录下的文件
//序列化——写出对象
for(int i=0;i<stuArray.length;i++)
{
writeStuObject(fileName,stuArray[i]);
Student stuArray1=(Student)readStuObject(fileName);
System.out.println(stuArray1);
}
}
private static Student readStuObject(String fileName) throws FileNotFoundException, IOException, ClassNotFoundException {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(fileName));
Student stuArray1=(Student)ois.readObject();
ois.close();
return stuArray1;
}
private static void writeStuObject(String fileName, Student stuArray) throws FileNotFoundException, IOException {
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(fileName));
oos.writeObject(stuArray);
oos.close();
}
}
- 学习JAVA
- Java学习
- JAVA学习
- 学习Java
- java学习
- Java学习?
- Java学习
- JAVA学习
- JAVA学习
- java学习
- 学习java
- JAVA学习
- JAVA学习
- java学习
- 学习java
- 学习JAVA
- 学习JAVA
- java学习
- 常用服务器模型总结
- 个人关于数据结构的看法
- 字符排序
- 图论感想
- GNU Libtool
- java学习
- IP地址转换
- BNU Choosing number 矩阵快速幂
- dll依赖跟踪(sxstrace.exe)
- vs2010编译配置Boost 1.54.0
- Linux脚本文件相关操作
- 再次使用cin失败解决方法
- Java 多线程下载
- Oracle