views:

228

answers:

2

Hello all,

Im reading from a file (data.bin) using the following approach -

 fis1 = new FileInputStream(file1);
 String data;
    dis1 = new DataInputStream(fis);
    buffread1=new BufferedReader(new InputStreamReader(dis1));
    while( (data= buffread1.readLine())!=null){

}

Now im getting the io exception of read error. Now im guessing that I probably am not able read the data in the file as they are stored in the following format.

 
#SP,IN-1009579,13:00:33,20/01/2010,    $Bœ™šAe%N                        
B\VÈ–7$B™šAciC                        B]|XçF [s  +    ýŒ         01210B3âEªP6@·B.

the above is just one line of the file and i want to read every line of that file and carry out operation on the data that is read. Any pointers on how the above can be accomplished would be of great help.

Cheers

+3  A: 

That look like part of binary data. You don't want to read it entirely as character data. Rather use an InputStream instead of Reader to read binary data. To learn more about the IO essentials, consult Sun's own IO tutorial.

BalusC
A: 

I guess what you want is just: (DataInputStream expects some objects that have been serialized as an array of bytes...)

    buffread1=new BufferedReader(new FileReader(file1));
    while( (data= buffread1.readLine())!=null){

}
Pierre
Filereader read the data alright, but not correctly. When i do a hex conversion of the data string read by the filereader, i see it being converted in the wrong fashion, i see a lot of'fffd''s getting appended to many byts that are converted to hex
ping
@ping, that's because it's not character data. Hex is funny as representation of binary files, but that isn't an excuse to *actually* read them as character data for business purposes. The xFFFD characters by the way are specific to the output/display thing which you're using to view them (it namely usually means that it cannot find a font/glyph for the fictive character associated with the underlying bytes), not to hex/binary/whatever.
BalusC