Sab Sab - 2 months ago 12
Java Question

Unable to read whitespaces when reading from a dat file in java

I am trying to read a dat file and store column names.
The first line in the file is separated by spaces.
The following code is unable to split the column names from the line

FileInputStream fis=new FileInputStream("file.dat");
BufferedReader br=new BufferedReader(new InputStreamReader(fis));
String line=br.readLine();``
String k[]=line.split(" ");


if i print k[0] it prints the entire line and the same does not happen if its a text file. What should i do to read the white space

Answer

It's possible that the fields are separated by whitespace other than the simple " " space. You can try splitting by any kind of whitespace with:

String k[]=line.split("\\s");

If this doesn't work either, the data is not separated by whitespace. Try using a hex editor/viewer to see what the separator character is.