Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

input /output problem

  Asked By: Ronnie    Date: Jun 23    Category: Java    Views: 597
  

i am getting trouble in reading this file.

my code
--------
import java.io.*;

class ReadBytes
{

public static void main(String[] args)

{

int b;

try

{

FileInputStream fin = new FileInputStream ("x.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??? is it not possible to read data usuing
DataInputStream only?????

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Ora Hanson     Answered On: Jun 23

DataInputStream uses Unicode to represent the data. Check the
documentation. I am not sure if attachments are allowed on this list,
but I try to attach the file  you need. Otherwise you can generate the
file using the following program derived from yours ;)

Regarding the EOF you could use EOFException, but I am sure there are
more elegant ways.

If you want to read data  files which have the format you mentioned in
your email I suggest to use BufferedReader + StringTokenizer.

 
Answer #2    Answered By: Angel Watkins     Answered On: Jun 23

the code  u have given i tested that, and that is creating a output
file indeed but that is not readable....is that a binary file???
i.e

outfile.writeInt(12);
> outfile.writeInt(7);
> outfile.writeInt(8);
> outfile.writeInt(8);
> outfile.writeInt(4);


this is producing an output file  but there is no 12 7 8 8 and
4 ....some non-readblae characters are there......

 
Answer #3    Answered By: Burkett Bernard     Answered On: Jun 23

> the code  u have given i tested that, and that is creating a output
file  indeed but that is not readable....is that a binary file???
Yes, you can call the file binary. The file contains the 12, 7, 8, 8, 4
but on 4 bytes, that's why you cannot read  them. Have a look at the hex
values and you will see the numbers there.

 
Didn't find what you were looking for? Find more on input /output problem Or get search suggestion and latest updates.




Tagged: