java - 文件为空时显示错误消息的问题

标签 java eclipse serialization

我想在文件第一次为空时以红色显示错误消息 但我的问题是在这个方法 ois.available() 中它总是返回 0 并且文件不为空

信息:

Person 类:

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;
    }
}

Room 的类和主要测试:

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

第一次执行获得结果:=> OK

文件为空

************************ 显示****************************

第二次执行获得结果:=> KO

文件为空

************************ 显示****************************

编辑

当我更改了 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;
    }

返回

第一次执行获得结果:=> KO

************************ 显示****************************

第二次执行获得结果:=> OK

************************ 显示****************************

姓名:名字1

姓名:FristName2

姓名:FristName3

最佳答案

Room中,将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;
}

顺便说一句,我也将 addPerson 方法中声明 oos 的方式更改为此(以清理一些):

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();
            }
        }
    }
}

关于java - 文件为空时显示错误消息的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28112715/

相关文章:

eclipse - 相当于Intellij IDEA中的Eclipse Classpath容器

linux - SVN 不断提示我输入密码并拒绝缓存我的凭据

c++ - 将 uint8_t 转换为其二进制表示

java - 如何比较两个java sql日期,一个在列表中,另一个刚刚输入

java - java中静态变量的类级锁

java - 指南:范围相关问题

java - XMLStreamWriter.writeStartElement(前缀,本地名称,命名空间URI): no binding of prefix to namespace

java - Eclipse 更新轴

c# - protobuf-net 忽略 [KnownType] 并需要添加 ProtoInclude

kotlin - 我可以使用具有多个密封类级别的 kotlinx 序列化器作为父类和嵌套调用吗?