[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

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;
String startname = data.readUTF();
data.seek(end - ((stringsize + datalength) * (a + 1)));
String endname = data.readUTF();
System.out.printf(startname+"\t"+"\t"+"\t"+ endname+"\n");
} catch (IOException e) {
  • 3
  • 2
1 Solution
How are you writing the file?  Can you print that code?
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]);
bigmdawgAuthor Commented:
I can't use any collections to store and read the data.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

It's not a collection.

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

How are you storing the data anyway ?
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.
If you look at the RandomAccessFile api you'll see a readLine() which returns a String.


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.

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now