• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Text File Parse - Code comments please

 
Jonathan Storey
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all.

Would someone please have a look at my class that simply parses a text file and converts it to a List<String>. I am really after code comment (i.e. its ok but you should - that kind of comment) as I am trying to improve. Thanks,





[HENRY: Added Code Tags]
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can add some validations like
- check whether the file exists or not.
- file type

to make your program robust.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's generally a good idea to close files in a finally block, to make sure it's going to be closed.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In practice I'd usually rather see this as a single method:

public List<String> parseFileToList(File theFile) throws IOException { ...

Aside from the missing validation mentioned above, I'd say this is well done.
 
Jonathan Storey
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for the replies. One question though, what is the best way to check the file type? I assume that you did not mean the .ext as a check?

 
Lucas Franceschi
Ranch Hand
Posts: 106
Mac Mac OS X Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try something like this


understanding this code:
  • first, on line 4, we get the name of your file.
  • then, on line 5, we get the last position of the string "." on your string fileName
  • so if the index is not 0 (line 6) then you make a substring (line 10).

  • to make the substring, you get by first character (that will be included in the resulting string) the dotIndex (that holds the position of the last dot (.)) and you dont specify a last character, so that you'll have a string that goes from "." to the end of the fileName, now you have the extension.

    of course it may not be the best way, but I always use this.

    regards.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic