读取对象直到在java中结束文件 [英] Reading Objects until End of File in java

查看:237
本文介绍了读取对象直到在java中结束文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个程序,用户可以:1)添加一个人到联系人(姓名,电话,电子邮件),2)从联系人中删除一个人,3)从联系人中读取所有人。



我这样做的方式是我要求用户进行选择并分别进行操作。为了写作,我只需将一个对象写入文件。对于删除,我想我会问用户的姓氏将用作关键字(因为我使用的是TreeMap),并将删除键值(对象)。



所以我在这里阅读时遇到问题。我试图读取像这样的对象:

  public void readContact()
{
TreeMap< ;字符串,联系人> contactMap = new TreeMap< String,Contact>();
尝试
{
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(
new FileInputStream(file))); (in.available()> 0)//这行不读
{
Contact c =(Contact)in.readObject();
contactMap.put(c.getLastName(),c); (Map.Entry联系:contactMap.entrySet())
{
联系con = contactMap.get(contact.getKey());
}


System.out.println(con.getLastName()+,+ con.getFirstName()+:+ con.getPhoneNumber()+\t+ con.getEmail());

$ b catch(Exception e)
{
System.out.println(Exception caught);


$ / code $ / pre

请不要建议像<$ c $因为:

    $ b while(true)直到得到 EOFException $ b
  1. 这不是什么异常处理是我相信的

  2. 之后我还有更多的事情要做,所以我不能让程序终止'


解决方案


请不要在while(true) EOFException


这正是我的建议。当您搜索答案时,根据这样的任意标准限制解决方案空间是相反的。


因为:

>

这不是什么异常处理是我相信的

当一个API你正在调用抛出异常,就像这样,你别无选择,只能抓住它。无论你怎么想'异常处理是什么',你都应该接受设计者在设计API时所设想的
$ b


在这之后我还有更多的事情要做,所以我不能让程序终止'

所以不要终止它。 Catch EOFException,关闭输入并跳出循环。



我看到更浪费编程时间的代价更高在'什么异常处理是'比我真的可以信任。'

I'm trying to write a program where the user can: 1) Add a person to the contact (name, phone, email), 2) Remove a person from the contacts, 3) Read all from contact.

The Way I'm doing this is I'm asking for the user for their choice and respectively does whatever. For writing, I simply write an object to the file. For removing, I think I'll be asking the user for "last name" which will be used as the KEY (since I'm using a TreeMap)and will remove the value (object) at the key.

So I'm having a problem with reading here. I'm trying to read the object like so:

public void readContact()
{
  TreeMap<String, Contact> contactMap = new TreeMap<String, Contact>();
  try 
  {
    ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(
                                                   new FileInputStream(file)));

    while( in.available() > 0 ) //This line does NOT read  
    {
      Contact c = (Contact)in.readObject();
      contactMap.put(c.getLastName(), c);           
    }

    for(Map.Entry contact : contactMap.entrySet() ) 
    {
      Contact con = contactMap.get( contact.getKey() );
      System.out.println( con.getLastName() + ", " + con.getFirstName() + ": " + con.getPhoneNumber() + "\t" + con.getEmail());
    }
  }
  catch(Exception e)
  {
    System.out.println("Exception caught");
  } 
}

Please do not suggest doing something like while(true) until I get the EOFException because:

  1. that isn't what exception handling is for I believe
  2. I still have more things to do after this so I can't have the program terminating'

解决方案

Please do not suggest doing something like while(true) until I get the EOFException

That is exactly what I suggest. When you are searching for answers it is counter-productive to circumscribe the solution space according to arbitrary criteria like this.

because:

that isn't what exception handling is for I believe

When an API that you are calling throws an exception, as this one does, you don't have any choice but to catch it. Whatever you may think about 'what exception handling is for', you are subject to what the designers of the API thought when they designed the API.

I still have more things to do after this so I can't have the program terminating'

So don't terminate it. Catch EOFException, close the input, and break out of the loop.

I have seen more costly programming time wasted over 'what exception handling is for' than I can really credit.

这篇关于读取对象直到在java中结束文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆