This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

reading from a binary file  RSS feed

Patricia Smith
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 9406
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Dale Seng
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!