• Welcome to Valhalla Legends Archive.
 

reading from a file

Started by touchstone, January 18, 2004, 01:12 AM

Previous topic - Next topic

touchstone

hi , i am getting trouble in reading this file.


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);
}
 
}

}


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

Adron

#1
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?

Banana fanna fo fanna

Java classes read big-endian by default.

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.
- Hostile is sexy.

Tuberload

Quote from: touchstone on January 18, 2004, 01:12 AM
hi , i am getting trouble in reading this file.


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);
}
 
}

}


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????

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

Quotequestion3
---------------

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

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???
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/

Quotethanks
Quote"Pray not for lighter burdens, but for stronger backs." -- Teddy Roosevelt
"Your forefathers have given you freedom, so good luck, see you around, hope you make it" -- Unknown