当文件第一次为空时,我希望显示红色的错误消息 但我的问题是在这个方法中ois.available()它总是返回0而文件不是空的
import java.io.Serializable;
public class Person implements Serializable{
private static final long serialVersionUID = -1378867624677931843L;
public long idPersonne;
public String FristName ;
public String LastName ;
public String Adresse;
public Personne()
{
idPersonne ++;
}
String str1 = "";
public String toString()
{
str1 += "Frist Name : "+FristName +"\n";
return str1;
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.EOFException;
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.util.ArrayList;
public class Room {
public ArrayList<Person> Lpersonne= new ArrayList<Person>();
ObjectInputStream ois;
ObjectOutputStream oos;
public Room()
{
}
public String toString()
{
String str="********************** Display **************************** \n";
try {
ois = new ObjectInputStream(
new BufferedInputStream(
new FileInputStream(
new File("Person.txt"))));
if(ois.available()==0)
{
System.err.println("File is empty");
}
else
{
while(true)
{
str +=(((Person)ois.readObject()).toString());
}
}
}catch (EOFException ex1) {
} catch (ClassNotFoundException e) {
} catch (IOException e) {
}
return str;
}
public void addPerson(Person p1) throws EOFException
{
Personne P2=null;
String str="";
Lpersonne.add(p1);
try{
oos = new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream(
new File("Person.txt"))));
for(Person P: Lpersonne){
oos.writeObject(P);
}
oos.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) throws EOFException {
Room r = new Room();
System.out.println(r);
Person p1 = new Person();
p1.FristName = "Firstname1" ;
p1.LastName = "LastNam1";
p1.Adresse = "Adresse1";
r.addPersonne(p1);
Person p2 = new Person();
p2.FristName = "FristName2" ;
p2.LastName = "LastName2";
p2.Adresse = "Adresse2";
r.addPerson(p2);
Person p3 = new Person();
p3.FristName = "FristName3" ;
p3.LastName = "LastName3";
p3.Adresse = "Adresse3";
r.addPerson(p3);
}
}
文件为空(红色)
**********************显示************************ ****
**********************显示************************ ****
名字:名字1
名字:FristName2
名字:FristName3
档案为空
**********************显示************************ ****
档案为空
**********************显示************************ ****
当我更改了available()方法并拔出File对象并调用file.length()== 0时:
public String toString()
{
String str="********************** Display **************************** \n";
try {
File f = new File("Person.txt");
ois = new ObjectInputStream(
new BufferedInputStream(
new FileInputStream(
f)));
// System.out.println(ois.available());
if(f.length()==0)
{
System.err.println("File is empty");
}
else
{
while(true)
{
str +=(((Personne)ois.readObject()).toString());
}
}
}catch (EOFException ex1) {
} catch (ClassNotFoundException e) {
} catch (IOException e) {
}
return str;
}
它返回
**********************显示************************ ****
**********************显示************************ ****
Nom:Firstname1
Nom:FristName2
Nom:FristName3
答案 0 :(得分:0)
在会议室中,将toString()
更改为:
public String toString() {
String str = "********************** Display **************************** \n";
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("Person.txt"));
while (true) {
try {
str += (((Person) ois.readObject()).toString());
} catch (EOFException ex1) {
break;
}
}
} catch (EOFException ex1) {
System.err.println("File is empty\n");
} catch (ClassNotFoundException e) {
} catch (IOException e) {
}finally {
if (ois != null) {
try {
ois.close();
}catch (IOException e) {
System.err.println("Error closing ois");
e.printStackTrace();
}
}
}
return str;
}
正如旁注,我改变了你在方法oos
中声明addPerson
的方式(以清理它):
public void addPerson(Person p1) throws EOFException {
Lpersonne.add(p1);
ObjectOutputStream oos = null;
try {
FileOutputStream out = new FileOutputStream("Person.txt");
oos = new ObjectOutputStream(out);
for (Person P : Lpersonne) {
oos.writeObject(P);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (oos != null) {
try {
oos.close();
}catch (IOException e) {
System.err.println("Error closing out oos");
e.printStackTrace();
}
}
}
}