Hi,

I'm a bit new to java and I would like to make a program that save's and load the saved data that the user had inputted in my java program.

For example I done a sort of a Bank System and when I close my program and restart it all over again the data is lost... Please can you give me a model program?

Thanks
Tabone

Recommended Answers

All 9 Replies

What kind of data, you want to save and do you have any "homework" restrictions concerning how the data will be saved?

There is the easy way using the Serializable interface. The code is only a few lines, but you will not learn much.

And the hard way would be to write the values of the attributes of your classes to a text file. With this way you will learn how to write and read from files.

What kind of data, you want to save and do you have any "homework" restrictions concerning how the data will be saved?

There is the easy way using the Serializable interface. The code is only a few lines, but you will not learn much.

And the hard way would be to write the values of the attributes of your classes to a text file. With this way you will learn how to write and read from files.

Kind of data?? such as Strings, double etc..? if so I will only input Strings ,Double and whole numbers(integers).There are no restrictions.. Doing this only to gain more knowledge about the subject

As JavaAddict said, Serialized objects give you a good option if you're looking to store a few bits

It won't save nulls, but you can create an object to swap for nulls when you're reading/writing

Check out the java.io.ObjectOutputStream

a simple

File f = new File("c:\\myobj.obj");
  ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
  out.writeInt(1);
  out.flush();
  out.close();

will write out to file 1 integer, you can read it in with ObjectInputStream.readInt

You can write multiple objects and read them in (in sequence) later when needed. Check out making your own objects serializable and using 'writeObject()' on the ObjectOutputStream

ie. you can have a session object that's serializable and store all the information you need, write it out, then read it in with readObject() and cast it to the object that was written at a later date to continue the session

As JavaAddict said, Serialized objects give you a good option if you're looking to store a few bits

It won't save nulls, but you can create an object to swap for nulls when you're reading/writing

Check out the java.io.ObjectOutputStream

a simple

File f = new File("c:\\myobj.obj");
  ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
  out.writeInt(1);
  out.flush();
  out.close();

will write out to file 1 integer, you can read it in with ObjectInputStream.readInt

You can write multiple objects and read them in (in sequence) later when needed. Check out making your own objects serializable and using 'writeObject()' on the ObjectOutputStream

ie. you can have a session object that's serializable and store all the information you need, write it out, then read it in with readObject() and cast it to the object that was written at a later date to continue the session

Cannot understand a thing =S

Cannot understand a thing =S

The answer was given: "Will write one(1) out to the file". But here is a little more detail on what is happening on each line

File f = new File("c:\\myobj.obj"); -> Create a new file in the C:\ drive called myobj.ob
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f)); -> Initalizes Object Output Stream (used for writing) to the file defined above
out.writeInt(1); -> Writes the number 1 to the file
out.flush(); -> Removes anything in the output buffer
out.close(); -> Closes the connection to the file

The answer was given: "Will write one(1) out to the file". But here is a little more detail on what is happening on each line

File f = new File("c:\\myobj.obj"); -> Create a new file in the C:\ drive called myobj.ob
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f)); -> Initalizes Object Output Stream (used for writing) to the file defined above
out.writeInt(1); -> Writes the number 1 to the file
out.flush(); -> Removes anything in the output buffer
out.close(); -> Closes the connection to the file

Thanks man!!

Now how to get the data fom the file myobj.obj to use it in your program?

Another thing that I would like to add, that might not be clear. That code using the ObjectOutputStream and ObjectInputStream, is best used to save whole objects. Not only int numbers: out.writeInt(1) If you look at the API, there is a method: ObjectOutputStream.writeObject(Object obj) that takes as argument any object. So if you have a class with many attributes, you can call only that method and that's it.
For reading just call: ObjectInputStream.readObject() . It returns an Object and all you have to do is cast to the right one.
These classes have examples:
ObjectOutputStream
ObjectInputStream

Remember in order to do that, the class that you are trying to save must implement the Serializable interface. And if your class has other classes as attributes, those must also implement that interface:

class CustomObject1 implements java.io.Serializable {
    private CustomObject2 cust = null;
    private String s = null;
    private int i = 0;
}
class CustomObject2 implements java.io.Serializable {

}

If you look at the API of String class you will see that it already implements that interface. And primitive types "int" can be written with no extra work. So you are good to go.
That means that not all classes of the JAVA API can be written with that way if they don't implement Serializable.

You can write collections as well:

SomeObject [] array = new SomeObject[5];
....
....

File f = new File("filename");
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
out.writeInt(array)
out.close();
...
...
SomeObject [] otherArray = (SomeObject [])in.readObject();

Or you can save Vectors.

Another thing that I would like to add, that might not be clear. That code using the ObjectOutputStream and ObjectInputStream, is best used to save whole objects. Not only int numbers: out.writeInt(1) If you look at the API, there is a method: ObjectOutputStream.writeObject(Object obj) that takes as argument any object. So if you have a class with many attributes, you can call only that method and that's it.
For reading just call: ObjectInputStream.readObject() . It returns an Object and all you have to do is cast to the right one.
These classes have examples:
ObjectOutputStream
ObjectInputStream

Remember in order to do that, the class that you are trying to save must implement the Serializable interface. And if your class has other classes as attributes, those must also implement that interface:

class CustomObject1 implements java.io.Serializable {
    private CustomObject2 cust = null;
    private String s = null;
    private int i = 0;
}
class CustomObject2 implements java.io.Serializable {

}

If you look at the API of String class you will see that it already implements that interface. And primitive types "int" can be written with no extra work. So you are good to go.
That means that not all classes of the JAVA API can be written with that way if they don't implement Serializable.

You can write collections as well:

SomeObject [] array = new SomeObject[5];
....
....

File f = new File("filename");
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
out.writeInt(array)
out.close();
...
...
SomeObject [] otherArray = (SomeObject [])in.readObject();

Or you can save Vectors.

OOO now I understand THANKS MAN!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.