user3776738
user3776738

Reputation: 254

JAVA: how to read very long lines in a tabstop delimitered txt.file

I want to read a .txt file which is table in the tabstop format like this:

1    ABC    short text
2    DEF    very very long text....
3    GHI    short text
4    JKL    short text

The problem is, that not the full line is read,when the text is very long (this means just a few KB not GB or so ;-)). I use this code to do the reading:

try {
     InputStream fis = new FileInputStream(file.getAbsolutePath());
     InputStreamReader isr = new InputStreamReader(fis, Charset.forName("ISO-8859-1"));
     BufferedReader br = new BufferedReader(isr);

     while ((line = br.readLine()) != null) {
           String[] values = line.split("\t", -1); // don't truncate empty fields
           System.out.println(values[0] + " " + values[1] + " " + values[2]);                  
     }
}

To explain the problem better: the result of the above table is this (which should not be):

1    ABC    short text
2    DEF    very very lo

Upvotes: 0

Views: 569

Answers (1)

Raedwald
Raedwald

Reputation: 48702

You could write a more sophisticated parser, which reads one character at a time until, placing the characters into a buffer until it encounters a TAB or EOL. At a TAB, process the buffer, increment the column count and then clear the buffer. At an EOL, process the buffer, set the column count to 0 and then clear the buffer.

Upvotes: 1

Related Questions