Java Tutorial #5 Serializable - Writing objects to a file and accessing them.
Previous tutorial https://steemit.com/java/@nicetea/java-tutorial-4-combining-reading-and-writing-functions
In this tutorial, we are going to dig into object oriented programming and storing those created objects into a file for later usage.
Firstly, a simple variant of an accounting code(like on Facebook, bank account...). The explanation is below the code, as usual.
Account class
public class Account implements Serializable {
private int nr;
private String name;
public Account(int nr, String name){
this.nr = nr;
this.name = name;
}
public int getNr() {
return nr;
}
public void setNr(int nr) {
this.nr = nr;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
So public class Account implements Serializable { is defining a new Java class called "Account" which is using the interface Serializable, so we can use serialization on this class.
Firstly, we are going to create the attributes(nr,name) first as usual. Then, we are creating a public constructor, which lets us easily initialize an object for later usage.
Below, the getters and setters, which basically either return a specific attribute or sets them.
Main Class
package Serializable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class Main {
/**
* @param args
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException,
IOException {
Account account;
Account bob = new Account(12345, "Bob Burger");
Account anna = new Account(6789, "Anna Annanas");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
"accounts.txt"));
oos.writeObject(bob);
oos.writeObject(anna);
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
"accounts.txt"));
try {
while (true) {
String result = "";
account = (Account) ois.readObject();
result += "Account Nr: "+account.getNr();
result += " | Account name: "+account.getName();
System.out.println(result);
}
} catch (Exception e) {
}
ois.close();
}
}
Firstly, we will create 3 Account objects, where 1 is empty for later usage. The 2 other accounts(bob,anna) are getting filled, by using the constructor we created earlier. For example the account bob has now the number "12345" and his name is "Bob Burger".
Then, we create an ObjectOutputStream, so we now have a file, where our objects are getting stored. After that, we are writing those objects to the file with the command oos.writeObject();
So good so far, now we want to see if it all worked out right? If we look at the accounts.txt file, it is filled, but doesn't look helpful when viewing it in some text editor. So let's use java to load the objects in the file and print out the contents!
This time, we are going to create an ObjectInputStream, so we are able to read the file accounts.txt. Then we create a while loop, surrounded by a try{} catch[} for error handling. Inside the while loop, we declare an empty string, for data storage and use the empty account object we declared at the beginning of the file. With the command (Account) ois.readObject();, we are reading the current object and casting it as a type of Account, so that we can use the getters.
Lastly, we use the .getNr(); and the .getName(); method, to read out the objects data and print them out.
Result
Stay tuned!
Congratulations! This post has been upvoted from the communal account, @minnowsupport, by Niceplayer from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, and someguy123. The goal is to help Steemit grow by supporting Minnows and creating a social network. Please find us in the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.
If you like what we're doing please upvote this comment so we can continue to build the community account that's supporting all members.