• Post Reply Bookmark Topic Watch Topic
  • New Topic

Initialize Object from File  RSS feed

 
Johnny Venter
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm successfully reading from a file that has the following format:

firstname
lastname
age

Here's an example:

Johnny
Appleseed
23

This is done by wrapping FileReader within a BufferedReader. I either store these values in String variables or an array of type String.

I also have Person class:


I would like to create a Person object based on the file contents. Here's what I've tried so far:



Instead of the array option, i've also tried:


Both options do work, but seems very "un-elegant". I don't know whether the file contents -> array option is okay. Is there a better option, should I use serialization or another data structure?
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should consider how you are reading from the file, and how you are creating objects.

Using a plain simple buffered reader has become a bit low‑level since the development of Scanner. If you go through the documentation for Scanner, there is an example about reading longs from a text file. You can expand that example with myScanner.next() and myScanner.nextInt() (and another hasNextXXX method) so you can get the two names and age from the file. Then you can pass them to the constructor. That is one way to do it. You will doubtless have realised there are lots of other ways to do it.

If you create a Person(String[]) constructor, that is brittle. What if the array doesn't have exactly 3 elements? What if the 3rd element (index=2) isn't an int? All sorts of things could go wrong.

What are you doing about files which have multiple lines in? If you use the next method of a Scanner that permits you to have your details on the same or on different lines.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not think serialisation will help you.
 
Johnny Venter
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell for the Scanner hint. I got it working and it seems cleaner now.

Here is my updated Person class constructor:



Here is my implementation code:



I need to add some checking on the file contents, but this is better than the array option.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you have reason to believe the names will be several words, on the same line, consider using next() rather than nextLine(). That of course depends on the names being one “token” each.
The reason is that there are problems with nextLine after nextAnythingElse. All will be well until you try to create a second Person object from the same text file. If things go wrong, never fear. I know the solution.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!