File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reading from a binary file" Watch "reading from a binary file" New topic

reading from a binary file

Patricia Smith

Joined: Feb 19, 2005
Posts: 3

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"
at Source)
at Prog9b.main(

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:


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 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 =, 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.

Joe Ess

Joined: Oct 29, 2001
Posts: 9191

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.

[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.

I agree. Here's the link:
subject: reading from a binary file
It's not a secret anymore!