We can read a file with FileReader function available in Java as
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
Once data loaded into memory from file, now read it line by line
String line = br.readLine();
while(line!=null)
{
pieces = line.split(separator); //for meta keywords we use mostly comma
//now the string is converted to array and pieces are available in index (es) 0, 1, 2 etc.
}
No doubt, if this split way suits your case so you will use it but here i am uploading an example about Serialization in Java. In which object knows how to read or write themselves to streams. The importance and reason to use serialization is that objects get created on heap and have some values therefore objects have some state in memory so we need to save and restore that state for objects. But by using java serialization we can save this effort because java serialization automatically cares about it.
Attachment
Code Highlights:
Files:
There are three files
1- Main File
2- Object Write Class File
3- Object Read Class File
Functions & Description:
public void printInfo(String[] args) {
// TODO code application logic here
JOptionPane.showMessageDialog(null , “name: ” + name + “address:” +address + “Roll no:” + rollNum);
}
This function get arguments and display a dialog box .
public static void main(String args[])
{
PersonInfoClass pWrite = new PersonInfoClass(“fahad mahmood”, “wapda town”, “mc100200752”);
try
{
FileOutputStream fos = new FileOutputStream(“mc100200752.dat”);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(pWrite);
out.close();
fos.close();
}
catch(Exception ex)
{
System.out.println();
}
}
This function is from WriteEx.java file. It creates an object from the PersonInfoClass and pass three parameters to constructor function. Then create a file mc100200752.dat with the function FileOutputStream and then load the data in ObjectOutputStream. Now Java will write this object with the help of writeObject function, here the essence of serialization that we don’t need to explode the string and then write to a file. Just use writeObject and then close the object and file respectively. Java will maintain the split behavior automatically.
public static void main(String args[])
{
try
{
FileInputStream fin = new FileInputStream(“mc100200752.dat”);
ObjectInputStream in = new ObjectInputStream(fin);
PersonInfoClass pRead = (PersonInfoClass)in.readObject();
pRead.printInfo(args);
in.close();
fin.close();
}
catch(Exception ex)
{
System.out.println();
}
}
This function is from ReadEx.java in which we load the mc100200752.dat file in object “in” with the help of FileInputStream and ObjectInputStream. Then read the loaded object with readObject. Now we have choice, either to print it or need to process for customized representation or not.
I have this post about java serialization will give you a clearer picture of its usage and benefit. I coudn’t write in much details because of busy schedule, but if you have any query, just nudge me i will back to you.
Thanks
Last updated: March 19, 2014