RSS Forums RSS

Modifying a file.

Please support our Java advertiser: Programming Forums
Thread Solved
Reply
Posts: 6
Reputation: xandres is an unknown quantity at this point 
Solved Threads: 0
xandres xandres is offline Offline
Newbie Poster

Modifying a file.

  #1  
Nov 22nd, 2008
I need to make a seating reservation system for my assignment and I have to manage .dat files. I have to store all Student objects in a file. When a student is registered, a new object is created and I need to add it to the archive. But as far as I know, you can not edit a serializable file in a "direct" form, so what I am trying to do is to generate an array of objects (temporary) with all the objects from the file, enter the new object registered and copy each object from the array to the file again.

I have the methods to do that but I don't know where to call the methods to perform the operations. Also I don't know in which class do I have to create the ArrayList.
Here is my code, I just put the classes relevant to my question:

  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class Estudiante extends Persona implements Serializable {
  5. private String idNo;
  6. private byte gradEst;
  7. private String codeSeat;
  8.  
  9. public Estudiante(String name, char sex, int age, String location, String idNo, byte gradEst, String codeSeat){
  10. super(name, sex, age, location);
  11. setIdNo(idNo);
  12. setGrade(gradEst);
  13. setCodeSeat(codeSeat);
  14. }
  15.  
  16. public void setIdNo(String idNo){
  17. this.idNo=idNo;
  18. }
  19.  
  20. public void setGrade(byte gradEst){
  21. this.gradEst=gradEst;
  22. }
  23.  
  24. public void setCodeSeat(String codeSeat){
  25. this.codeSeat=codeSeat;
  26. }
  27.  
  28. public String getIdNo(){
  29. return idNo;
  30. }
  31.  
  32. public byte getGrado(){
  33. return gradEst;
  34. }
  35.  
  36. public byte getCodeSeat(){
  37. return codeSeat;
  38. }
  39.  
  40. public static ArrayList<Student> generateBackup(){
  41. FileInputStream f3 = null;
  42. ObjectInputStream f4 = null;
  43. ArrayList<Student> students = new ArrayList<Student>();
  44. try{
  45. f3=new FileInputStream("C:\\Documents and Settings\\Daniel Garcia\\Escritorio\\Andres\\Final\\students.dat");
  46. f4=new ObjectInputStream(f3);
  47. while(true){
  48. Student e=(Student)f4.readObject();
  49. students.add(e);
  50. }
  51.  
  52. }
  53.  
  54. catch(EOFException e){
  55. System.out.println("End of file");
  56. }
  57.  
  58. catch(IOException e){
  59. System.out.println("Error reading file");
  60. }
  61. catch(ClassNotFoundException e){
  62. System.out.println("Error in type of class");
  63. }
  64. finally{
  65. try{
  66. f4.close();
  67. f3.close();
  68. }
  69. catch(IOException e){
  70. System.out.println("Error closing file");
  71. }
  72. }
  73. return students;
  74.  
  75. }
  76.  
  77. public static void writeBackup(ArrayList<Student> students){
  78. FileOutputStream f1 = null;
  79. ObjectOutputStream f2 = null;
  80. try{
  81. f1=new FileOutputStream("C:\\Documents and Settings\\Daniel Garcia\\Escritorio\\Andres\\Final\\students.dat");
  82. f2=new ObjectOutputStream(f1);
  83. for(Student e : students){
  84. f2.writeObject(e);
  85. }
  86.  
  87. }
  88. catch(IOException e){
  89. System.out.println ("Error saving file");
  90. }
  91. finally{
  92. try{
  93. f2.close();
  94. f1.close();
  95. }
  96. catch(IOException e){System.out.println ("Error closing file");
  97. }
  98. }
  99. }
  100.  
  101. }

  1. import java.io.*;
  2. import java.util.*;
  3. class Main{
  4.  
  5. public static void main (String args[]){
  6. boolean menu = false;
  7. Student x = null;
  8. do{
  9. System.out.println("Welcome!!");
  10. System.out.println();
  11. System.out.println("What do you want to do?");
  12. System.out.println("1) Separate a seat");
  13. System.out.println("2) Cancel a seat");
  14. System.out.println("3) Another here");
  15. System.out.println("4) Another here");
  16. System.out.println("5) Another here");
  17. System.out.println();
  18. char ans = Lectura.readChar();
  19. System.out.println();
  20.  
  21. switch(ans){
  22. case '1':
  23. Student.generateBackup();
  24. System.out.println("Name");
  25. String name = Lectura.readString();
  26. System.out.println("Age");
  27. byte age = Lectura.readByte();
  28. System.out.println("Sex M/F");
  29. char sex = Lectura.readChar();
  30. System.out.println("Id. No.");
  31. String idNo = Lectura.readString();
  32. System.out.println("Grade of Studies");
  33. byte gradEst = Lectura.readByte();
  34. System.out.println("Location");
  35. String location = Lectura.readString();
  36. System.out.println("Here is a Seating chart");
  37. Seatings.displaySeatings();
  38. System.out.println("Select your seat");
  39. String codeSeat = Lectura.readString();
  40.  
  41. x = new Estudiante(name,sex,age,location,idNo,gradEst,codeSeat);
  42.  
  43.  
  44. //I need to put x in the arraylist but I don't know how to do that, any ideas?? Estudiante.writeBackup();
  45.  
  46. menu=true;
  47. break;
  48.  
  49.  
  50. }
  51. }
  52. while(menu);
  53.  
  54.  
  55. }
  56. }

I translated from Spanish to English some parts of the code so you understand it with more precision.
Thanks for the help in advance.
AddThis Social Bookmark Button
Reply With Quote  
Posts: 1,036
Reputation: BestJewSinceJC is a glorious beacon of light BestJewSinceJC is a glorious beacon of light BestJewSinceJC is a glorious beacon of light BestJewSinceJC is a glorious beacon of light BestJewSinceJC is a glorious beacon of light 
Solved Threads: 120
BestJewSinceJC BestJewSinceJC is offline Offline
Veteran Poster

Re: Modifying a file.

  #2  
Nov 22nd, 2008
Making a class serializable in java means that you can read it from and write it to a file in binary form. So when your program exits, that is when you should be writing all of your objects to the file. When your program starts, you read them in from the file. When you read from a "serializable file", it creates your Objects for you. When you write to a "serializable file" you are essentially storing your Objects in a special form so that they can be read back in, as Objects, when you start your program the next time. I don't know a whole lot about the details of how it works (for example, I'm not sure when or how exactly the Objects are created), but that's the general idea. Hope that helps. If not, and you were already aware of that, I'll be glad to help you work out the details once you reply.
Last edited by BestJewSinceJC : Nov 22nd, 2008 at 3:51 am.
Reply With Quote  
Posts: 6
Reputation: xandres is an unknown quantity at this point 
Solved Threads: 0
xandres xandres is offline Offline
Newbie Poster

Re: Modifying a file.

  #3  
Nov 22nd, 2008
Yes I understand the mechanism about writing/reading objects from a file. But basically this is my problem:
- I have a file that has objects in it.
- I want to write a new object in that file when I run the program, but if I do that "directly" using the writeObject, then the other objects will be lost (overwriting file).
- So what I am doing is copying every object from the file to an array of objects, so there will no lost objects after writing the new one. Then I copy each object from the array to the file. I have the methods to do that (as you can see in my Estudiante class) but I don't know how to call the methods in Main class to perform this actions.

Estudiante.generateBackup(); >> this line is working OK (line 23)
Estudiante.writeBackup(); >> this is causing an error when I compile (line 44):

ERROR: Main.java:51: writeBackup(java.util.ArrayList<Student>) in Estudiante cannot be applied to ()
Estudiante.writeBackup();

Thanks for your reply.
Last edited by xandres : Nov 22nd, 2008 at 4:15 am.
Reply With Quote  
Posts: 947
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 106
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Modifying a file.

  #4  
Nov 22nd, 2008
You'll probably have to reposition your read-stream cursor after each object read, otherwise you won't retrieve the right byte value from the file to match the data type(s) for the Object you are creating.
Reply With Quote  
Posts: 1,143
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 121
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Modifying a file.

  #5  
Nov 22nd, 2008
Just a thought, instead of writing each object after serializing one by one to the file, why don't you just serialize the entire ArrayList and write it to a file.
And when you need to add any data to the file, get the ArrayList back from the file, add the object to the ArrayList and serialize this ArrayList overwrite the older file with the file containing the new ArrayList
Last edited by stephen84s : Nov 22nd, 2008 at 6:57 am.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote  
Posts: 947
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 106
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Modifying a file.

  #6  
Nov 22nd, 2008
Originally Posted by stephen84s View Post
Just a thought, instead of writing each object after serializing one by one to the file, why don't you just serialize the entire ArrayList and write it to a file.
And when you need to add any data to the file, get the ArrayList back from the file, add the object to the ArrayList and serialize this ArrayList overwrite the file with the older file.


Unfortunately, the implementation of ArrayList<T> looks [something] like this--

  1. //... necessary imports
  2.  
  3. public class ArrayList<T> extends List<T> implements Collection<T>, Serializable{
  4.  
  5. private transient Object[] elements = null; // key point @_@
  6.  
  7. public T get(int position){
  8. return (T)elements[position];
  9. }
  10. }
Reply With Quote  
Posts: 7,398
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 439
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Modifying a file.

  #7  
Dec 7th, 2008
I felt a bump was in order for this one...

> Unfortunately, the implementation of ArrayList<T> looks
> [something] like this--

Doesn't seem unfortunate to me; as long as the state is persisted and can be recovered from the flattened representation of the object, it doesn't matter which approach is taken to serialize the object. In this case, the implementer chose not to serialize the array which backs the ArrayList but the individual elements for obvious reasons [hint: the size of the array is not always equal to the number of elements in the ArrayList ].

The only thing you need to look out for is that the objects contained in the ArrayList can be serialized [the concrete class implements either Serializable or Externalizable ].

If my memory serves me right, you posted something along the same lines in a thread you created; maybe it's time to go back and edit it. :-)
I don't accept change; I don't deserve to live.

Act from reason, and failure makes you rethink and study harder.
Act from faith, and failure makes you blame someone and push harder.

-- Eric Naggum RIP :-(
Reply With Quote  
Posts: 6
Reputation: xandres is an unknown quantity at this point 
Solved Threads: 0
xandres xandres is offline Offline
Newbie Poster

Re: Modifying a file.

  #8  
Dec 7th, 2008
Thanks to all who replied my question.
I already solved the problem and obtained a good grade from this project.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 696 | Replies: 7 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 10:23 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC