LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-14-2005, 08:20 PM   #1
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Rep: Reputation: 31
Serialized objects into Strings


Hello:

I have read that I can write a Java object and transform it into an Stream, so to write it into a file. I have:


Code:
public class Guy implements Serializable
{
     private String name;

      public Guy()
     {
            super();
            name = "me";
      }

      public String toString(){ return "Hey it's " + name; }
}
and then I can do this:

Code:
// put it into the file
FileOutputStream out = new FileOutputStream("serial.txt");
ObjectOutputStream s = new ObjectOutputStream(out);
s.writeObject(new Guy() );

// read it from the file

FileInputStream in = new FileInputStream("serial.txt");
ObjectInputStream reader = new ObjectInputStream(in);

Guy me = (Guy)reader.readObject();

System.out.println(me);
So in the file I basically have an String.... I was wondering, can I get that String without reading it from a file, so I can store it in a Database, and I can read and write the object like TEXT, keeping its state? how to get the String that the method writeObject() will write in the file?

Thanks in advance

Poeta

http://www.novacreations.net
 
Old 07-14-2005, 11:11 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
am i getting what you want... you want to write actually text to a file rather than objects?
if so, you would use BufferedReader for input and PrintWriter for output.

when you create a bufferedreader you must put it in a try/catch statement. the bufferedreader takes in 1 parameter, which is a File object, which that takes in a parameter of a string to the filename.

also for the printwriter, it must be in a try/catch statement. it takes one parameter, which is a bufferedwriter which takes in 1 parameter which must be a filewriter which takes in 1 parameter for a file object which again takes a parameter for a string of the filename.

heres an example of the printwriter (bufferedwriter is shorter):
Code:
...
outFile = new PrintWriter(new BufferedWriter(new FileWriter(new File("/path/to/file"))));
...
from there, both are pretty easy to use. to read from the input file simply inFile.readLine() and to write to the output file use outFile.println()
check sun's site for the documentation for each so you know all the other methods that are available (i think theres stuff like .print() or .readInt() or something, i havent used it in months).

edit: also be sure that you close your IO files appropriately

Last edited by nadroj; 07-14-2005 at 11:13 PM.
 
Old 07-14-2005, 11:19 PM   #3
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Original Poster
Rep: Reputation: 31
Hello:

thanks for your answer, but I believe I didn't quite explain myself ..... the:

FileOutputStream out = new FileOutputStream("serial.txt");
ObjectOutputStream s = new ObjectOutputStream(out);
s.writeObject(new Guy() );

gives me a file with a String in it, representing the serialization for that object right?, but my question is, how can I get that String, not in a file, but in a String datatype, maybe something like:

String serializedObject = something.Serialize(myObject);

I hope I did write well............ I kinda want a workaround in object persistence in databases.......... so if I have a group of objects, I could set them into Strings and save them in databases, and then retrieve them...

Thanks!

so I could save that String somewhere else, and then "deserialize" it
 
Old 07-14-2005, 11:24 PM   #4
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
are you just storing strings? if so then my above method will work

oh wait, no.. u said you were writting 'Guy's to the file, right?

your code:
Code:
FileOutputStream out = new FileOutputStream("serial.txt");
ObjectOutputStream s = new ObjectOutputStream(out);
s.writeObject(new Guy() );
DOESNT write a string to the file.. it writes an object of type Guy and stores it as binary data into the file. you cant then go read the file and extract just a string from it, because you dont know where it would be in the file or how long, etc.
you have to, as you did, read in the object and cast it to the type (Guy) then get whatever info you saved in the file, back by accessing the objects methods

edit: king crimson is great

Last edited by nadroj; 07-14-2005 at 11:25 PM.
 
Old 07-14-2005, 11:28 PM   #5
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Original Poster
Rep: Reputation: 31
hey

so basicaly, I do need first the file, and then read it? hehehe mmmmmm while writting the serialized representation, will reading it as a file actually give me a valid String?

PS Yes!!! Matte Kudasai!
 
Old 07-14-2005, 11:37 PM   #6
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
i didnt understand that..
your first sentence, yes.. the next one, i dont get.

are you asking if you can just open the file, read its raw contents and extract from that what you are looking for? (which is a string or whatever, as you made it seem).
if so, the answer would be no.
try it yourself, open up a text editor and view the contents of your output file (serial.txt).. can you see the string in there? probably not. its binary data representing an object.
again, youll need to read this object first from the file, then using that object youll have to access whatever your looking for (string?) using it's methods (ie, myGuy.toString(), myGuy.getName().. whatever)
 
Old 07-14-2005, 11:54 PM   #7
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Original Poster
Rep: Reputation: 31
hehehehe I did see the file and I do see a String.......... some readable, and some chars like squares...... mmmmmmmmm I was thinking I could override the object persistence so I could transform any given object and then save it in mySql in a Text variable.............. but I believe it can't be done =(

thanks!
 
Old 07-15-2005, 12:00 AM   #8
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
you wanted to store a java object in a SQL text variable?
and yes.. when you read the file, you will of course notice afew characters here and there that you know what they represent or would say, but thats because you _know_ what theyre supposed to say.. either way, its not the string.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
java objects zaicheke Programming 3 03-23-2005 06:21 AM
Ndiswrapper - "Driver is serialized. This will not work" pdcorcoran Linux - Wireless Networking 4 09-14-2004 01:18 PM
lost objects gurra59 Linux - Newbie 4 09-07-2003 04:29 PM
Objects in Java? mikeshn Programming 0 06-11-2002 10:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration