[ジャワ] 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 + "]";
}
}



eはクラスファイルのコードを適用し送信することができます “MyStudent” クラスで “ファイル” K先生? プロジェクトは、eが適用されすぎると. 私は感謝します
リンクポスト:https://www.cachhoc.net/2014/10/04/java-ghi-them-doi-tuong-vao-file-trong-java-append-object-file-exist-java/
私は、あなたの上にすべてのコードを書きました.
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. sửa lại rồi
MyStudent student;
Object obj;
同時に ((obj = ois.readObject()) != nullを) {
もし (obj instanceof MyStudent) {
student = (MyStudent) OBJ;
のSystem.out.println(student);
}
}