简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM