Valhalla Legends Forums Archive | Java Programming | reading from a file

AuthorMessageTime
touchstone
hi , i am getting trouble in reading this file.
[code]

import java.io.*;

class ReadBytes
{

public static void main(String[] args)

{

int b;

try

{

FileInputStream fin = new FileInputStream ("c:\\data1.txt");
DataInputStream infile = new DataInputStream(fin);


    b= infile.readInt();
   
System.out.print(b);




infile.close();
fin.close();


}

catch(IOException ioe)

{
   System.out.println(ioe);
}

}

}
[/code]

my data file
----------------
12 7 8 8 4

i am getting output

825368631

question 1
----------------
can you tell me how this output is produced??

question2
-------------
i was expecting output 12 as i have called readInt () only once.....why println() could not produce the correct result????

question3
---------------

if i want to read all the data from my file using while loop then how do i check end_of_file???


i do not want to use stringtokenizer or bufferreader etc but i want to stick to my code
and want to read the file .....is not it possible in this code using datainputstream only???


thanks
January 18, 2004, 7:12 AM
Adron
You're apparently reading the file binary - reading 4 bytes from the file and interpreting them as an integer.

How did I come to this conclusion?

Well, put 825368631 into calc and hit "Hex". That will give you 31322037. For those who don't read hex natively, 30..39 is '0' .. '9', and 20 is space. So that is really '12 7'.

The only thing that surprises me is that you didn't get 37203231, because that's the way an intel x86 cpu would typically interpret your string. What hardware/os are you running this on? Or is this a guaranteed property in the java classes?
January 18, 2004, 2:11 PM
St0rm.iD
Java classes read big-endian by default.
January 18, 2004, 3:56 PM
Hostile
Well, Your problem is from using the DataInputStreams readInt method. If you read the API JavaDoc for this method you see that it returns you the next 4 bytes. ( http://java.sun.com/j2se/1.4.2/docs/api/java/io/DataInputStream.html ) I'm not totally sure what you want to actually do (besides printing out) with the data once you get it so I'm just going to say choose a different method. If you want to read the entire and get each value then perhaps return the entire thing and parse each value?

In responce to Adron, good job for picking that up hehe and But in all pure java cases (no system specific calls or what not) hardware is not an issue of any sort due to the VM though different between chipsets compiles the java bytecode the same way.
January 19, 2004, 7:32 AM
Tuberload
[quote author=touchstone link=board=34;threadid=4777;start=0#msg40022 date=1074409965]
hi , i am getting trouble in reading this file.
[code]

import java.io.*;

class ReadBytes
{

public static void main(String[] args)

{

int b;

try

{

FileInputStream fin = new FileInputStream ("c:\\data1.txt");
DataInputStream infile = new DataInputStream(fin);


    b= infile.readInt();
   
System.out.print(b);




infile.close();
fin.close();


}

catch(IOException ioe)

{
   System.out.println(ioe);
}

}

}
[/code]

my data file
----------------
12 7 8 8 4

i am getting output

825368631

question 1
----------------
can you tell me how this output is produced??

question2
-------------
i was expecting output 12 as i have called readInt () only once.....why println() could not produce the correct result????[/quote]

I'm sure Adron and Hostile gave you enough to answer these.

[quote]question3
---------------

if i want to read all the data from my file using while loop then how do i check end_of_file???[/quote]

Most file i/o methods will return -1 when the end of file is reached.

[quote] i do not want to use stringtokenizer or bufferreader etc but i want to stick to my code
and want to read the file .....is not it possible in this code using datainputstream only???
[/quote]
You would not have to use a stringtokenizer to do this at all, but I would recommend using buffers because it will significantly increase your programs performance. http://java.sun.com/developer/technicalArticles/Programming/PerfTuning/

[quote]thanks[/quote]
January 19, 2004, 7:26 PM

Search