473,320 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Reading & Writting Binary File

72
Got a question about reading and writing binary files.

I know that to write a value(integer) to a binary file I can use the fileoutputstream to do it.
Expand|Select|Wrap|Line Numbers
  1. try 
  2. {
  3.         // Create an output stream to the file.
  4.         FileOutputStream file_output = new FileOutputStream (outFile);                                                              
  5.  
  6.         //for(int i = 0; i < cInt.length; i++)
  7.  
  8.         file_output.write(100);                
  9.         file_output.write(200);                
  10.         file_output.write(300);                
  11.  
  12.         // Close file when finished with it..
  13.         file_output.close ();
  14. }
But I cant seem to read back from the file these values using the fileinputstream

Expand|Select|Wrap|Line Numbers
  1. int bInt = 0;
  2. try 
  3. {
  4.     // Wrap the FileInputStream with a DataInputStream            
  5.     FileInputStream file_input = new FileInputStream (inFile);
  6.     DataInputStream data_in    = new DataInputStream (file_input);
  7.  
  8.     while(true) 
  9.     {
  10.         try 
  11.         {                    
  12.             bInt = data_in.readInt();
  13.         }
  14.         catch (EOFException eof) 
  15.         {
  16.             //end of file
  17.             break;
  18.         }
  19.  
  20.         System.out.println("Test: "+bInt);
  21.     }
  22.  
  23.     data_in.close ();
  24. catch(IOException e) 
  25. {
  26.     System.out.println ( "IO Exception =: " + e );
  27.     System.exit(0);
  28. }
Any idea where I went wrong? Thanks in advance :)
Nov 14 '07 #1
1 2882
KWSW
72
ok nvm... solved it :)
Nov 14 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: fdunne2 | last post by:
How do you write/read a list of siganl points into a c program? each point is on a seperate line.
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
30
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
6
by: jjhall | last post by:
I need to open and read a file that has a combined ascii and binary format. Don't ask me why it is this way; I think it is kind of stupid, but I didn't write the program that generated the file. ...
5
by: Vijay | last post by:
Hi All, I have written below code to read and write a file (Big file). But every time, one extra character(may be NULL char) has been written in output file. Can any one please tell me why it...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
3
by: Ahmad Jalil Qarshi | last post by:
Hi, I have a text file having size about 2 GB. The text file format is like: Numeric valueAlphaNumeric values Numeric valueAlphaNumeric values Numeric valueAlphaNumeric values For example...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.