Reading integers from a sequential file.

I have to write names with 4 scores each to a sequential file and then read them back in regular order on the left and reverse order on the right. I have where the names will write back but can't get the scores with them. I tried using data.readInt() in a loop to get the scores but it won't work. Any pointers in the right direction will be appreciated. Thanks.

public void readfile() {
RandomAccessFile data = null;
File f = new File("F://fileouputtest.dat");
long end = f.length();
try {
data = new RandomAccessFile(f, "r");
for (int a = 0; a < 20; a++)
{
int offset = (stringsize + datalength) * a;
data.seek(offset);
String startname = data.readUTF();
data.seek(end - ((stringsize + datalength) * (a + 1)));
String endname = data.readUTF();
System.out.printf(startname+"\t"+"\t"+"\t"+ endname+"\n");
}
data.close();
} catch (IOException e) {
e.printStackTrace();
}
}
bigmdawgAsked:
Who is Participating?
 
Kelvin_KingCommented:
If they way you are writting to the file is in this matter.

PersonName Score1 Score2 Score3 Score4
...

Then you just need to read in the whole line from the text file.

once you done that, do a split:

String Scores[] = line.split(" ");
int scoresInt[] = int[4];
and convert each of the score to int.

for(int i = 1; i < Scores.length; i ++) //start from 1 because the first string is the name of person
  scoresInt[i -1] = nteger.parseInt(Scores[i]);
0
 
TimYatesCommented:
How are you writing the file?  Can you print that code?
0
 
bigmdawgAuthor Commented:
I can't use any collections to store and read the data.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Kelvin_KingCommented:
It's not a collection.

I'm just saying read the data as a line.

How are you storing the data anyway ?
0
 
bigmdawgAuthor Commented:
I mean I can't use arrays,list, stacks, etc.. to read the data back. I'm writing it with a data.writeUTF() and data.writeInt() statements into a .dat file.
0
 
Kelvin_KingCommented:
If you look at the RandomAccessFile api you'll see a readLine() which returns a String.

http://java.sun.com/j2se/1.5.0/docs/api/java/io/RandomAccessFile.html

You can try and use that from read the data back from the file.

The arrays has nothing to do with reading the data, i'm only using it to store the processed data.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.