ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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
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:
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
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
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?
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)
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 =(
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.