BufferedReader and Scanner's nextLine() seem to be helping a little too much by removing all trailing whitespace. I need to preserve columns, which at the moment are allowed to be empty values, but hesitate to loop through each row using next() or getBytes() identifying tab characters since there could potentially be millions of rows with hundreds of columns.
Are there alternatives to these two methods that I'm missing for reading lines? Are there flags or any other options to set in these methods to preserve whitespace? Do I simply force the user to use none-blank fields? I'm not alone in trying to preserve whitespace am I?
I have a problem with it when it's reading from a file. I have this code
import java.lang.*;
import java.util.*;
import java.io.*;
public class stringTest
{
public static void main (String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new FileReader("wtf.txt"));
String l = br.readLine();
while (l != null) {
System.out.println(l.split("\t").length);
l = br.readLine();
}
}
}
wtf.txt contains
h\tu\tr\tf\n
o\tm\tg\t\t\n
And the output is
4
3
Additionally, if I add a line anywhere that is all tabs, ie
h\tu\tr\tf\n
\t\t\t\t\t\n
o\tm\tg\t\t\n
The output is
4
0
3
I don't think it's an issue with split because if I use the code
String s = "w\tt\tf\t\t\n";
System.out.println(""+s.split("\t").length);
String s1 = "w\tt\tf\tx\n";
System.out.println(""+s1.split("\t").length);
String s2 = "\t\t\t\t\t\t\n";
System.out.println(""+s2.split("\t").length);
The output is
5
4
6