1

I am trying to read info from a file and create objects out of that information. Every 6 or so lines of the file is a different unit, meaning that the first set of lines are relevant to object A, the next set to object B, and so on.

I can read from the file and create my object just fine--for the first set. My problem is that I don't know how to get the reader to pick up from the spot it left off at when creating the next object...

(Note: the read() method which creates the file is part of the new object being created, not in a main() or anything like that). Here are the relevant bits of code:

The driver:

public class CSD{

    public static void main (String[] argv){
        Vector V=new Vector(10);
        CoS jon=new CoS();
        jon.display();
    }//end main
}

which calls CoS, whose constructor is:

public CoS(){
    try{
        String fileName=getFileName();
        FileReader freader=new FileReader(fileName);
        BufferedReader inputFile=new BufferedReader(freader);
        this.read(inputFile);
        setDegree(major);
        setStatus(credits);
    } catch(FileNotFoundException ex){
    }//end catch
}

Which calls both read() and getFileName():

public void read(BufferedReader inputFile){
    try{
        int n;
        super.read(inputFile);
        String str=inputFile.readLine();
        if (str!=null){
            n=Integer.parseInt(str);
            setCredits(n);
            str=inputFile.readLine();
            setMajor(str);
        }//end if
    }catch(IOException ex){}
}//end method

public String getFileName() {
    Scanner scan = new Scanner(System.in);
    String filename;
    System.out.print("Enter the file name and path ==> ");
    filename = scan.nextLine();
    System.out.println("");
    return filename;
}

Thanks in advance, guys!

Bruno Vieira
  • 3,884
  • 1
  • 23
  • 35
Jo.P
  • 1,139
  • 4
  • 15
  • 35
  • 2
    Why don't you pass a starting line number to each constructor as well? Otherwise, It would be a better idea to just do one file read, and create all the objects at once, passing those values to the constructor instead. – Joel Oct 31 '12 at 17:42
  • Agree. Otherwise, look here: http://stackoverflow.com/questions/4305094/java-reading-strings-from-a-random-access-file-with-buffered-input – Andrea Ligios Oct 31 '12 at 17:43
  • Joel, how do I get the reader to start from the # I would pass it? That's a good idea, how do I implement it? – Jo.P Oct 31 '12 at 17:48
  • Why don't you just do one file read, and pass the values to a constructor instead? That is much more efficient.. Otherwise you would just prolly have to do a readLine() and ignore the contents until you reach the given line.. – Joel Oct 31 '12 at 17:54
  • The problem is that this is a project for class and the reading has to be done as part of the objects, not in the main(), so I can't just read through it and pass parameters to the object to deal with :( . Regarding reading through the lines until a desired point is hit...how can I get it to know when is the important line? I'm thinking about it, hopefully something will pop into my head... – Jo.P Oct 31 '12 at 18:00
  • 1
    You pass the important line number to the constructor from main.. And I don't know why you would ever do it that way.. It is a poor implementation of object oriented programming. – Joel Oct 31 '12 at 18:07

1 Answers1

3

Why not use ObjectInputStream and ObjectOutputStream? Or any kind of real serialization?

javadoc: http://docs.oracle.com/javase/6/docs/api/java/io/ObjectOutputStream.html

example code: http://www.javadb.com/writing-objects-to-file-with-objectoutputstream

Basically, since you write your objects to a file and want to take care of the lines where they are located, I'll suggest a few other serialization alternatives.

One is the Object * Stream - you create a ObjectStream on a File and just write objects thru it. Later when you read, you read the objects in the reverse order you wrote them and they will come back just as you wrote them.

Another is to implement Serializable. Remember that transient keyword? Use it on fields you do not want to save to the file.

And then there's the raw "by hand" approach where you save only the things you want to save and reconstruct the objects later by passing these initialization values to their constructor. Kinda like people suggested that you make the file line a argument to the ctor :)

EDIT: guess writing with Object*Streams requires you to implement Serializable or Externalizable.

but if the example code isn't clear enough, ask :)

Shark
  • 6,513
  • 3
  • 28
  • 50
  • 1
    Could you please explain these things? I'm not so well versed in reading-approaches. I'll start to search on google for these terms, but if you could explain/give a little example, that would be very helpful... – Jo.P Oct 31 '12 at 18:04