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.