Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Writing and reading from/to a serialized file through Hash Table in Java

This section illustrates you how to read and write from/to a serialized file through the hash table in Java.

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

Writing and reading from/to a serialized file through Hash Table in Java

     

This section illustrates you how to read and write from/to a serialized file through the hash table in Java. This section provides an example with the complete code of the program. Following program has the facility if the specified serialized file does not exist the the program creates the serialized file otherwise. This program first write to the specified serialized file if it exists otherwise read all contents by de-serializing the file.

Code Description:

FileOutputStream fileOut = new FileOutputStream("HTExample.ser"):
Above code creates an object "fileOut" of the FileOutputStream with it's constructor which takes a file name which contains ".ser" extension that determines for the serialized file either which has to be created or written with the specified value in a hash table in the program. 

Here is the code of the program:

import java.util.*;
import java.io.*;

public class SimpleSerialization{
  private static void doSave(){
  System.out.println();
  System.out.println("+------------------------------+");
  System.out.println("| doSave Method  |");
  System.out.println("+------------------------------+");
  System.out.println();
  Hashtable<String,Object> h = new Hashtable<String,Object>();
  h.put("string""Oracle / Java Programming");
  h.put("int"new Integer(36));
  h.put("double"new Double(Math.PI));
  try{
  System.out.println("Creating File/Object output stream...");
  FileOutputStream fileOut = new FileOutputStream("HTExample.ser");
  ObjectOutputStream out = new ObjectOutputStream(fileOut);
  System.out.println("Writing Hashtable Object...");
  out.writeObject(h);
  System.out.println("Closing all output streams...\n");
  out.close();
  fileOut.close();
  }
  catch(FileNotFoundException e){
  e.printStackTrace();
  }
  catch(IOException e){
  e.printStackTrace();
  }
  }
  private static void doLoad(){
  System.out.println();
  System.out.println("+------------------------------+");
  System.out.println("| doLoad Method  |");
  System.out.println("+------------------------------+");
  System.out.println();
  Hashtable h = null;
  try{
  System.out.println("Creating File/Object input stream...");
  FileInputStream fileIn = new FileInputStream("HTExample.ser");
  ObjectInputStream in = new ObjectInputStream(fileIn);
  System.out.println("Loading Hashtable Object...");
  h = (Hashtable)in.readObject();
  System.out.println("Closing all input streams...\n");
  in.close();
  fileIn.close();
  }
  catch(ClassNotFoundException e){
  e.printStackTrace();
  }
  catch(FileNotFoundException e){
  e.printStackTrace();
  }
  catch(IOException e){
  e.printStackTrace();
  }
  System.out.println("Printing out loaded elements...");
  for(Enumeration e = h.keys(); e.hasMoreElements();){
  Object obj = e.nextElement();
  System.out.println("  - Element(" + obj + ") = " + h.get(obj));
  }
  System.out.println();
  }
  
  public static void main(String[] args){
  doSave();
  doLoad();
  }
}

Download this example.

Advertisements

Share on Google+Share on Google+

Writing and reading from/to a serialized file through Hash Table in Java

Posted on: April 16, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Writing and reading from/to a serialized file through Hash Table in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads