vivarem logo
/ Vivarem / Computers and IT / IT Education / online / tutorial / Java / OOP / 10 /
There are 0 sub sections

You can add Your Link here with New Link
Java Object Oriented Programming - 10

Serialization and Miscellaneous topics

Serializing
Is the technique of saving the current Object state.For an Object to be serialised the minimum condition is the class of object to be serialized should implement Serializable interface.
package com.vivarem.examples;
import java.io.*;
class Jser implements Serializable{
int a,b;
public Jser(int a,int b){this.a=a;this.b=b;}
public int sum(){ return a+b;}
}
public class JavaSerial{
public static void main(String[] a)throws IOException, ClassNotFoundException{
if(a[0].equals("write")){
FileOutputStream fout=null;

try{
Jser j=new Jser(100,2);
fout= new FileOutputStream("jx"); //Create anew file output stream
ObjectOutputStream os=new ObjectOutputStream(fout);//create new object outputstream
os.writeObject(j);//write to stream
}catch(IOException ioe){ System.out.println("failed writing to file"+ioe);}
finally{fout.close();//close the stream
}
}else if(a[0].equals("read")){
FileInputStream fin=null;
try{
Jser j;
fin= new FileInputStream("jx");//open the filestream for reading
ObjectInputStream os=new ObjectInputStream(fin);//create object stream for reading
j=(Jser)os.readObject();//read the object
System.out.println("sum from serialized object"+j.sum());
}catch(IOException ioe){ System.out.println("failed writing to file"+ioe);}
finally{fin.close();//close the stream
}
}
}
}


Reading from console from j2se 5.0
You may want write an interactive program by reading input from console. for this Scanner is the easiest class. It is possible to read multiple words and numbers from a line with scanner.
Scanner s= new Scanner(System.in);
String param= s.next();// read string
int value=s.nextInt();//read int
s.close(); //close it after use

ArrayList an Easy way for arranging data j2se 5.0
ArrayList is a natural extension to normal arrays discussed earlier programming can be much more easier by using Arraylist in place of Array
the following code illustrates an ArrayList

package com.vivarem.examples;
import java.util.*;
public class JavaArrayList{
public static void main(String[] a){
ArrayList list = new ArrayList();
for(int i=0;i<10;i++)
list.add(i,(int)(java.lang.Math.random()*100));// adding data to list
Collections.sort(list);//sorting list with Collections.sort
for(Integer i:list)//only from j2se5.0 i contains value at current position
System.out.println(i);
}
}

Apart from this it contains many usefull function for dataitem manipulation
e.g. list.contains(object) will return true if the element is present in list, similarly list.indexOf(object) will give the index of object if present -1 otherwise.

HashTable arranging data in key value pair
HashTable is a fast way of storing and retrieving data as key value pair objects.The following exmple illustrates this

package com.vivarem.examples;
import java.util.*;

public class JavaHashTable{
public static void main(String[] a){
Hashtable ht= new Hashtable();
ht.put(1001,"Antony");//add data in key value pair Objects
ht.put(1002,"Bu");
ht.put(1003,"Harry");
System.out.println(ht.containsKey(1002)?ht.get(1002):"not found");//checks if the Key is presentif it is present prints the value
}
}


Download example files here
< Exception Handling |
Content


You can Add a Classified here with New Classified