Welcome, Guest. Please login or register.
Did you miss your activation email?
Pages: [1]   Go Down
  Print  
Author Topic: Save and load (Vector) object in Java  (Read 2458 times)
0 Members and 1 Guest are viewing this topic.
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« on: June 17, 2007, 01:23:16 PM »

Code
GeSHi (java):
  1. import java.io.FileInputStream;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.ObjectInputStream;
  6. import java.io.ObjectOutputStream;
  7. import java.util.Iterator;
  8. import java.util.Vector;
  9.  
  10. public class SaveLoad {
  11. static Vector vSave = new Vector();
  12. static Vector vLoad = new Vector();
  13.  
  14.  
  15. public static void main(String[] args) {
  16. vSave.add("http://www.engineeringserver.com/forum/");
  17. vSave.add("http://www.engineeringserver.com/t3hc0d3z/");
  18. vSave.add("http://www.engineeringserver.com/blog/category/general/");
  19.  
  20. /*
  21. Iterator itSave=vSave.iterator();
  22. while (itSave.hasNext())
  23. itSave.next();
  24.  
  25. System.out.println(vSave);
  26.  
  27. }
  28. */
  29. System.out.println();
  30.  
  31.  
  32. try {
  33. f_out = new
  34. FileOutputStream("myData.data");
  35. ObjectOutputStream obj_out = new
  36. obj_out.writeObject ( vSave );
  37.  
  38. } catch (FileNotFoundException e) {
  39. // TODO Auto-generated catch block
  40. e.printStackTrace();
  41. } catch (IOException e) {
  42. // TODO Auto-generated catch block
  43. e.printStackTrace();
  44. }
  45. System.out.println("---saved Data---");
  46.  
  47.  
  48.  
  49. System.out.println("---loading Data---");
  50. try {
  51. f_in = new
  52. FileInputStream("myData.data");
  53. new ObjectInputStream (f_in);
  54. Object obj = obj_in.readObject();
  55. if (obj instanceof Vector)
  56. {
  57. Vector v = (Vector) obj;
  58. //System.out.println(v);
  59. //System.out.println("--- ---");
  60.  
  61. try{
  62. vLoad.add((Vector) v);
  63. }
  64. System.out.println("WOPS nullpointerexcepton");
  65.  
  66. }
  67. }
  68.  
  69. } catch (FileNotFoundException e) {
  70. // TODO Auto-generated catch block
  71. e.printStackTrace();
  72. } catch (IOException e) {
  73. // TODO Auto-generated catch block
  74. e.printStackTrace();
  75. } catch (ClassNotFoundException e) {
  76. // TODO Auto-generated catch block
  77. e.printStackTrace();
  78. }
  79.  
  80.  
  81.  
  82. // Object o = vLoad.lastElement ();
  83. /*
  84. Enumeration enum = vLoad.elements();
  85. while(enum(hasMoreElements()){
  86.  Vector colNamese = (v)enum.nextElement();
  87.  //do something with colNames, maybe get enumeration and loop
  88.  //it through.
  89. */
  90.  
  91.  
  92.  
  93. Iterator itLoad=vLoad.iterator();
  94. while (itLoad.hasNext()) { // iterate through the elements
  95. Vector rowElm=(Vector)itLoad.next(); // each 'rows' element is a vector
  96. Iterator rowVals=rowElm.iterator(); // each vector contains the actual values
  97. System.out.println(); // print a newline for each row
  98. while (rowVals.hasNext()) { // iterate through single row
  99. Object cellValue=rowVals.next(); // single value; column name or SQL object (Integer etc.)
  100. System.out.print(cellValue); // print value
  101. //System.out.print('\t'); // tab to separate values
  102. System.out.print('\n'); // newline to separate values
  103. }
  104. }
  105.  
  106.  
  107.  
  108. // System.out.println("VECTOR-----" + o);
  109.  
  110. //System.out.println("VECTOR-----" + vLoad);
  111. }
  112.  
  113.  
  114. }
  115.  
Created by GeSHI 1.0.7.20


output:



---saved Data---
---loading Data---

http://www.engineeringserver.com/forum/
http://www.engineeringserver.com/t3hc0d3z/
http://www.engineeringser...om/blog/category/general/
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here