• Post Reply Bookmark Topic Watch Topic
  • New Topic

I/O Files  RSS feed

 
Ruth Jordan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to import/export files, and I'm not capable of doing it. I appreciate any help please.

The instructions that I have received are:

The class which is going to be imported/exported should have following things:

First:

Second:

Third:

Fourth:

So, my code is the following one:

And also, what is what I'm exporting here my Product or my Stock? Because this is the code that I'm supposed to use, but I don't really understand it.


When I execute the program, I receive following information in the nuevo2.txt file:
Ԁ

Please, what is what I'm doing wrong?.
Thank you for your help in advance.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will find a good explanation of serialisation in Burce Eckel's book Thinking in Java.
Simply: your class must implement the Serializable tagging interface, which is in the java.io package. So your class should startNext: all its fields which are not marked with the keywords transient or static are serialised. You use transient for something like a password which you want removed from the serialised version, and static fields are not elements of the class, so they are not serialised.
Next: All its fields which are reference types must also also implement Serializable. Many of the well‑known classes like Exception Integer HashSet and String already do implement Serializable.
I see your class should override the toString method, and should have an empty no‑arguments constructor. Those are common requirements whether you serialise things or not. As you know it is redundant to write super(); because the compiler will add it automatically if you dont't write one of the following as the first line in a constructor:
super(); super(something); this(); this(something);
I believe you need a public no‑arguments constructor to deserialise an object, but the toString method is a general requirement which all classes should override.

You usually have methods for serialising and deserialising. You will require special methods in certain circumstances which are described as special treatment in the documentation for Serializable. If you can't work out what that means, Eckel (op cit) explains it clearly. I can't remember myself and haven't got my Eckel here. Go through the Java® Tutorials section, and the documentation for ObjectInputStream and ObjectOutputStream(←links) too. You would write methods like this:-I shall leave you to work out why there are (...) after try. Hint: you will find something in the Java® Tutorials. I shall also leave you to work out why the deserialise method is usually static.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All fields which are primitives and not marked transient or static are eligible for serialisation.
You can never understand an object file by opening it with a text editor.
 
Ruth Jordan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,

I was not able to use this structure, the file was not correctly exported. I will try it again in the next weeks.

Nevertheless, I have found a video in youtube, I have followed the instructions, &.... this has worked perfectly. The code that this video specified is the following one:




My question now is, in class Serializadora I have following line:

But I don't want any reference of the file in this class, because I need to write different files from different classes. As this is a string, how can I write a reference if I write it in the other classes as




Thank you in advance.
 
Ruth Jordan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Because what I can execute, is the method, isn't it, but it has an objet as parameter, I could write:


but how can I include a String from different classes in a method of other different class?

Thank you.
 
Raydelto Hernandez
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ruth. You can just add another String parameter to the escribirObjeto method specifying the name of the file where you want to write the serialized object so you pass this String to the FileOutputStream constructor instead of the hardcoded value.

BTW, I was the one who created the Youtube video, I'm glad it helped you in your inquiry .

Ruth Jordan wrote:Hi,

Because what I can execute, is the method, isn't it, but it has an objet as parameter, I could write:


but how can I include a String from different classes in a method of other different class?

Thank you.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!