Reading integers from a sequential file.

Posted on 2006-04-09
Last Modified: 2010-03-31
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(); - ((stringsize + datalength) * (a + 1)));
String endname = data.readUTF();
System.out.printf(startname+"\t"+"\t"+"\t"+ endname+"\n");
} catch (IOException e) {
Question by:bigmdawg
    LVL 35

    Expert Comment

    How are you writing the file?  Can you print that code?
    LVL 13

    Accepted Solution

    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]);

    Author Comment

    I can't use any collections to store and read the data.
    LVL 13

    Expert Comment

    It's not a collection.

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

    How are you storing the data anyway ?

    Author Comment

    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.
    LVL 13

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Title # Comments Views Activity
    twoTwo  challenge 35 71
    squareUp  challenge 22 80
    Running Jira on Raspberry PI 2? 3 44
    Starting to learn JAVA, 7 34
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    This video teaches viewers about errors in exception handling.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now