jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes reading from a binary file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reading from a binary file" Watch "reading from a binary file" New topic
Author

reading from a binary file

Patricia Smith
Greenhorn

Joined: Feb 19, 2005
Posts: 3
Hi,

I'm trying to write to and then read from a binary file. I have 2 .java files. The first one writes to and the 2nd one reads from. I wrote to the file and now I'm testing my read program and here's the output i'm getting...

I should say that there should be 4 fields printing out: state, an int called FIPS, another int called population and the name of the town ( a string). I have no idea what that really long number is in the beginning of each line.


20971842097184 WY_?_?Yoder lll
20971842097184 PA_?_?Gordon lll
20971842097184 WY_?_?Yoder lll
20971842097184 NJ_?_?Somers Point lll
20971842097184 NM_?_?Sandia Heights lll
20971842097184 WY_?_?Yoder lll

Exception in thread "main" java.io.EOFException
at java.io.RandomAccessFile.readChar(Unknown Source)
at Prog9b.main(Prog9b.java:61)



Those _ �s between the ?�s are actually those weird boxes that come from reading binary data wrong. I�m not sure why it�s kicking out that initial long number either.

Here�s my latest revised code for the second file:

import java.io.*;

public class Prog9b {


public static void main(String[] args) throws IOException{

// Setting up needed variables

int line = -1;

Object[] aray = new Object[4];

File inFile = new File("patricia.bin");

RandomAccessFile rafIn = new RandomAccessFile(inFile,"r");

long len = rafIn.length();



String state;

char[] tempState;

int MAXLEN_STATE = 2;



int FIPS;

String tempFIPS = "";

int intTempFIPS = 0;

int MAXLEN_FIPS = 4;



int population;

String tempPop = "";

int intTempPop = 0;

int MAXLEN_POP = 4;



String name;

char[] tempName;

int MAXLEN_NAME = 60;



// Test that data got inserted

// Read line of data

byte[] buffer = new byte[70];

line = rafIn.read(buffer, 0, 70);



// While there are still lines left in the file

while (line != -1){



// Read state field.

tempState = new char [MAXLEN_STATE];

for (int i = 0; i < tempState.length; i++)

tempState [i] = rafIn.readChar();

state = new String (tempState);





// Read FIPS field.

/*byte [] tempFIPS = new byte [MAXLEN_FIPS];

for (int i = 0; i < tempFIPS.length; i++)

tempFIPS [i] = raf.readByte();

*/

FIPS = rafIn.readInt();





// Read population field.

population = rafIn.readInt();



// Read name field.

tempName = new char [MAXLEN_NAME];

for (int i = 0; i < tempName.length; i++)

tempName [i] = rafIn.readChar();

name = new String (tempName);



System.out.println(state + FIPS + population + name + "lll");

}

}

}



Line 61 is the name = new String (tempName) line.

Thanks!!
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8713
    
    6

You say that the output from the first program is not what you expected. Then you say your second program, which expects the CORRECT output from the first program, is not working correctly. I'd say that your problem is in the first program. Binary files aren't ment to be read in a text editor, so don't get freaked by the weird characters. If you really want to understand the data, use a hex editor.
BTW, use code tags (the buttons under the edit window on the post page) to preserve the formatting of your code. And remove the extra line feeds. Most people here won't read more than 20 or so lines of code, so if it's at all possible, narrow down where your problem is.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Dale Seng
Ranch Hand

Joined: Mar 22, 2004
Posts: 275
I don't usually use random access file. If you have a relatively small file, you might consider a buffered input stream and just read the whole thing at once.



--Dale--
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reading from a binary file
 
Similar Threads
File conversion program...
Confusion with Binary, Byte representations
still stuck in database
Java nio
File encryption / decryption retaining line feeds