0

Hello,

So I've got trouble with a program I'm working on. The program is supposed to register studends and teachers to a register, and when I close it, I want to save all data with DataInputStream/DataOutputStream. So when I open the program, I want to see what I've registered. It's currently looking like this:

public abstract class School {
     private String name, address;
     private int zipcode;
     School next;

     public School(String n, String a, int z) {
         name = n;
         address = a;
         zipcode = z;
     }

     public void writeObjectToFile(DataOutputStream dos) {
         try {
             dos.writeUTF(name);
             dos.writeUTF(address);
             dos.writeInt(zipcode);
             dos.flush();
         }
         catch(IOException e) {
             // methods
         }
     }

     public boolean readObjectFromFile(DataInputStream dis) {
         try {
              while(true){
              name = dis.readUTF();
              address = dis.readUTF();
              zipcode = dis.readInt();
              }
         }
         catch(EOFException eof) {
              try {
                 dis.close();
              }
              catch(IOException e) {
                 // methods
              }
         }
         catch(IOException e2) {
            // methods
         }
         return true;
     }
     
     public String toString() {
          // methods for output
     }   
}

class Students extends School {
     private String name, iClass;
     private int age;

     public School(String n, String a, int z, String na, String c, int age) {
         super(n,a,z);
         name = na;
         iClass = c;
         age = a;
     }

     public void writeObjectToFile(DataOutputStream dos) {
         try {
             dos.writeUTF(this.getClass().getName()); // for identifying the class. 
             dos.writeUTF(name);
             dos.writeUTF(iClass);
             dos.writeInt(age);
             dos.flush();
         }
         catch(IOException e) {
             // methods
         }
     }

     public boolean readObjectFromFile(DataInputStream dis) {
         try {
              while(true){
                  dis.readUTF(); // identify Student object/class
                  name = dis.readUTF();
                  iClass = dis.readUTF();
                  age = dis.readInt();
              }
         }
         catch(EOFException eof) {
              try {
                  dis.close();
              }
              catch(IOException e) {
                 // methods
              }
         }
         catch(IOException e2) {
            // methods
         }
         return true;
     }
     
     public String toString() {
          // methods for output
     }
     
}

// I also have similar codes for class Teacher.

This is my Register class. I'll copy/paste the readFromFile- and writeToFile methods.

public class Register {
     // methods for insert a object and write a list. 
     School first;
     
     public void writeToFile(String filename) {
         try {
             DataOutputStream dos = new DataOutputStream(new FileOutputStream(filename));
             School node = first;
             while(node != null){
                  next.writeObjectToFile(dos);
                  node = node.next;
             }
             dos.close();
         }
         catch(IOException e) {
             // methods
         }
     }

     public void readFromFile(String filename) {
         DataInputStream dis = null;
         try {
              dis = new DataInputStream(new FileInputStream(filname));
              while(true){
                  String objectType = dis.readUTF(); // supposed to read the object type. for example Student. 
                  School object = newObject(objectType); // create new object with information on file. 
                  object.readObjectFromFile(dis); // fill the object with data
                  register(object); //I have a register method. Nothing wrong with this..
              }
         }
         catch(EOFException eof) {
              try {
                 dis.close();
              }
              catch(IOException e) {
                 // methods
              }
         }
         catch(IOException e2) {
            // methods
         }
     }

     public School newObject(String objectType) {
         switch(objectType) {
             case "Students": return new Students("", "", 0, "", "", 0); // not sure what values to give here. values will be given when I insert from GUI class. 
             case "Teachers": return new Teachers("", "", 0, "", "", 0);
         }
         return null; 
     }
}

I also have a GUI class and a main method class. GUI class got these methods:

public void writeToFile( String filename )
{
     register.writeToFile(filname);
}

public void readFromFile( String filename )
{
     register.readFromFile(filname);
}

My error is:

java.io.IOException: Stream Closed
	at java.io.FileInputStream.read(Native Method)
	at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:337)
	at java.io.DataInputStream.readUTF(DataInputStream.java:589)
	at java.io.DataInputStream.readUTF(DataInputStream.java:564)
	at Register.readFromFile(Register.java:59)
	at GUIclass.readFromFile(GUIclass.java:215)
	at MainClass.main(MainClass.java:10)

on MainClass I have

gui.readFromFile("data.dat");

What I've done wrong? My readFromFile in register class is supposed to create a new object from the information in the file. Then I will fill this object with data with help of readObjectFromFile method. Then I'll add the object to the list.

Edited by Chario0z: n/a

3
Contributors
8
Replies
11
Views
5 Years
Discussion Span
Last Post by JamesCherrill
0

how can you have a School constructor at all, not to mention, how can you have one in your Students class?

you say it doesn't run? I doubt it even compiles. check the error messages you get, those you can solve, solve them, those you can't, paste them here, and we'll help you out.

0

I could just make a new file Student.java and paste the code there. This used to be a finish program without data I/O-strams. We had to extend it with data I/O-streams.

It compiles fine, and it runs. But when I open it again to check if my data is saved, I get the error message I pasted in my last post. That's why it doesn't run properly.

0

yes, but we can't see what is at line 10 of MainClass and the part of GUIClass you show is 9 lines, so how are we to know what you do on line 215?

point for my previous post in: you have a constructor in an abstract class... you can't instantiate an abstract class.
next to that, you have a constructor for that abstract class in a class that inherits mentioned abstract class. seriously, did that even compile?

0

on my mainClass i have that line i posted in the first post. also gui.readFromFile("data.dat");

on line 215 i have readFromFile method which you can find in the box with 9 lines.

this program compiles and runs fine, call my teacher stupid for the constructors and the abstrsct methods. it just dont want to run the way i want it to, something wrong with the readFromFile and readObjectFromFile methods i guess.

0

well .. having a Students class with a School constructor.
looks to me like you would have a problem there. AFAIK, it's just not possible to get that compiled.

0

What's the worry with abstract class constructors? They are perfectly valid and normal.
When you instantiate a concrete subclass the superclass constructors will always be called, even if the superclass is abstract. Just try a new B() with these classes:

abstract class A {
   A() {System.out.println("A constructor");}
   abstract void method();
}

class B extends A {
   B(){System.out.println("B constructor");}
   void method(){}
}
0

well yes, but in your class B, you did not have a:

public A(){

}

constructor, which is what he is doing. agreed, was a bit off on the use of abstract constructors, they do can have their purpose (which I overlooked), but in the Abstract class itself, not in the inheriting class.

0

That's line 55 in the original code? Yes, that's an error. Considering that it begins with a valid call to super(...) I guess that was just a small slip. It continues with other errors (eg line 59 - that's why variable names are important!) so that's obviously not a compilable version.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.