Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 18th, 2008, 12:40 PM
Registered User
 
Join Date: Oct 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to sanjh
Default Storing hash table into a file

Hi there,
Well can anyone tell me that how to store the content of a HASH TABLE's content into a file. There after how to see the contents of that file.

 
Old October 23rd, 2008, 10:17 PM
Authorized User
 
Join Date: Sep 2008
Posts: 87
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi
try this out

public class Storing {

   public static void main(String[] args) {
      store();
      fetch();
   }

   private static void store() {
        Hashtable h = new Hashtable();
        h.put("A", "chiru");
        h.put("B", "Nag");
        h.put("C", "Venki");

        try {

            FileOutputStream fileOut = new FileOutputStream("hello.txt");
            ObjectOutputStream out = new ObjectOutputStream(fileOut);

            out.writeObject(h);

            out.close();
            fileOut.close();

        } catch(FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
     private static void fetch() {

       Hashtable h = null;

       try {

           FileInputStream fileIn = new FileInputStream("Hello.txt");
           ObjectInputStream in = new ObjectInputStream(fileIn);

           h = (Hashtable)in.readObject();

           in.close();
           fileIn.close();

       } catch (ClassNotFoundException e) {
           e.printStackTrace();
       } catch(FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }

       for (Enumeration e = h.keys(); e.hasMoreElements(); ) {
           Object obj = e.nextElement();
           System.out.println(" Values: " + h.get(obj));
       }

   }

}



--Raj


 
Old February 11th, 2009, 08:24 AM
Registered User
 
Join Date: Feb 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Could you please let us know why you want to do this ???
 
Old April 10th, 2009, 11:31 PM
Friend of Wrox
 
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

The use of Enumeration is a bit of old style, foreach is better.





Similar Threads
Thread Thread Starter Forum Replies Last Post
storing serialized data in MySQL table ire MySQL 3 November 13th, 2008 06:27 AM
STL Linked List or Hash Table Bill_Thompson Visual C++ 0 November 14th, 2007 11:42 PM
Dynamic load of hash table jcwest Visual Basic 2005 Basics 0 October 29th, 2007 11:03 PM
Modifying table dynamically,storing dropdown value santy_capri Classic ASP Databases 0 July 25th, 2006 06:09 AM
Storing complete table rows in a single field KenNeedham Oracle 2 January 22nd, 2004 01:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.