Parsing a text file with a fixed format in Java

Posted by EugeneP on Stack Overflow See other posts from Stack Overflow or by EugeneP
Published on 2010-04-26T10:33:10Z Indexed on 2010/04/26 10:43 UTC
Read the original article Hit count: 239

Filed under:
|
|
|

Suppose I know a text file format,

say, each line contains 4 fields like this:

firstword secondword thirdword fourthword
firstword2 secondword2 thirdword2 fourthword2
...

and I need to read it fully into memory

I can use this approach:

open a text file
while not EOF
  read line by line
  split each line by a space
  create a new object with four fields extracted from each line
  add this object to a Set

Ok, but is there anything better, a special 3-rd party Java library?

So that we could define the structure of each text line beforehand and parse the file with some function

thirdpartylib.setInputTextFileFormat("format.xml");
thirdpartylib.parse(Set, "pathToFile")

?

© Stack Overflow or respective owner

Related posts about java

Related posts about text