[Java的] 记得对象添加到Java中的文件 – 追加对象文件存在于Java的
毕竟 从Java中的文件阅读对象, 还有,你需要写文件的详细数据很多问题. 通常我们只是声明
FileOutputStream f = new FileOutputStream(fileName,true);
但多次尝试后,我还是不. 搜索了一阵,除了额外的东西 2 这就是真实的,那么我们就需要覆盖的方法 writeStreamHeader 如下:
ObjectOutputStream oStream = new ObjectOutputStream(f) { protected void writeStreamHeader() throws IOException { reset(); } };
1. 快速编码测试
package vietSource.net.IOFile; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class ReadWriteObject { public static void main(String[] args) { File f = new File("student.dat"); FileOutputStream fo; ObjectOutputStream oStream = null; // ghi file lan 1 try { fo = new FileOutputStream(f); oStream = new ObjectOutputStream(fo); oStream.writeObject(new MyStudent("pham ha", 22)); oStream.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // ghi file lan 2 try { fo = new FileOutputStream(f, true); oStream = new ObjectOutputStream(fo) { protected void writeStreamHeader() throws IOException { reset(); } }; oStream.writeObject(new MyStudent("nguyenvanquan7826", 22)); oStream.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // doc file try { FileInputStream fis = new FileInputStream(f); ObjectInputStream inStream = new ObjectInputStream(fis); System.out.println(inStream.readObject()); System.out.println(inStream.readObject()); inStream.close(); } catch (ClassNotFoundException e) { System.out.println("Class not found"); e.printStackTrace(); } catch (IOException e) { System.out.println("Error Read file"); e.printStackTrace(); } } } class MyStudent implements Serializable { String name; int age; public MyStudent(String name, int age) { super(); this.name = name; this.age = age; } public String toString() { return "MyStudent [name=" + name + ", age=" + age + "]"; } }
2. 完整的代码应该使用
如果没有对象的文件或文件将被添加错误不存在, 因此,以确保所有情况下,你看到下面的代码:
package vietSource.net.IOFile; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class ReadWriteObject { // kiem tra file co object hay khong public static boolean hasObject(File f) { // thu doc xem co object nao chua FileInputStream fi; boolean check = true; try { fi = new FileInputStream(f); ObjectInputStream inStream = new ObjectInputStream(fi); if (inStream.readObject() == null) { check = false; } inStream.close(); } catch (FileNotFoundException e) { check = false; } catch (IOException e) { check = false; } catch (ClassNotFoundException e) { check = false; e.printStackTrace(); } return check; } public static void write(MyStudent s) { try { File f = new File("student.dat"); FileOutputStream fo; ObjectOutputStream oStream = null; // neu file chu ton tai thi tao file va ghi binh thuong if (!f.exists()) { fo = new FileOutputStream(f); oStream = new ObjectOutputStream(fo); } else { // neu file ton tai // neu chua co thi ghi binh thuong if (!hasObject(f)) { fo = new FileOutputStream(f); oStream = new ObjectOutputStream(fo); } else { // neu co roi thi ghi them vao fo = new FileOutputStream(f, true); oStream = new ObjectOutputStream(fo) { protected void writeStreamHeader() throws IOException { reset(); } }; } } oStream.writeObject(s); oStream.close(); } catch (IOException e) { e.printStackTrace(); } } public static void read() { try { File f = new File("student.dat"); FileInputStream fis = new FileInputStream(f); ObjectInputStream inStream = new ObjectInputStream(fis); Object s; int i = 0; while (true) { s = inStream.readObject(); System.out.println(++i + ":" + s.toString()); } } catch (ClassNotFoundException e) { } catch (IOException e) { } } public static void main(String[] args) { // ghi sinh vien 1 write(new MyStudent("nguyenvanquan7826", 22)); // ghi tiep sinh vien 2 write(new MyStudent("phamha", 22)); // doc file read(); } } class MyStudent implements Serializable { String name; int age; public MyStudent(String name, int age) { super(); this.name = name; this.age = age; } public String toString() { return "MyStudent [name=" + name + ", age=" + age + "]"; } }
A có thể gửi cho e xin cả code của file class “MyStudent” với Class “文件” được k ạ? nếu cho e xin project thì quá tuyệt. 谢谢
Link Bài:https://www.cachhoc.net/2014/10/04/java-ghi-them-doi-tuong-vao-file-trong-java-append-object-file-exist-java/
Code mình viết toàn bộ bên trên rồi đó bạn.
Bạn ơi phương thức read() trên luôn rơi vào IO Exception (cụ thể là EOF Exception ) , mặc dù vẫn chạy được nhưng liệu có cách nào xử lí lỗi EOF ko bạn?
Mình cũng cố gắng tìm nhưng chưa ra 🙂
thử như thế này xem
MyStudent student;
Object obj;
而 (真正) {
如果 ((obj = ois.readObject()) instanceof MyStudent) {
student = (MyStudent) OBJ;
的System.out.println(student);
}
}
sorry. 修订和
MyStudent student;
Object obj;
而 ((obj = ois.readObject()) != NULL) {
如果 (OBJ的instanceof MyStudent) {
student = (MyStudent) OBJ;
的System.out.println(student);
}
}