I have an Array of Objects called "person" and i need to save that data, Is there a way to output an array and then input it back into the program. I have tried simply outputting the raw String and int data that makes up each "person" and the program never writes that data to the specified txt file. Is there a method or function that can write raw object data and then retrieve it?
You should have a look to ObjectOutputStream and ObjectOutputStream and the Serializable interface. What you are trying to do is to serialize your data to a file and load (deserialize) it back again.
You can use standard Java serialisation. Another trivial solution is to use XStream, which will serialise your objects into/from XML. You don't need to implement specific interfaces or annotate your objects (by default) and you'll get a readable (in XML terms) serialisation, which can be useful.
One approach would be to use Serialization provided by Java. You need to implement the java.io.Serializable interface and write and read to and from the ObjectOutputStream and ObjectInputStream classes to store and retrieve data respectively.
You need serialization. Read this.
Related
I want objects of a same serializable class to serialize in the same file. I have tried to do it by putting everything in an array and then serialize by I want the objects to be serialized individually and saved into the same file
If you want to serialize multiple objects, why not serialize a collection of those objects? When you deserialize the collection you can access the objects again by iterating. Or if you have some unique identifer for the object you can put them in a map instead and serialize that.
Worth mentioning is that Java serialization will be going away in future with the newer versions. You are better off using a JSON serializer / de-serializer in my opinion, unless you of course are trying to hide the contents somehow. I use FasterXML myself and it works really great with POJOs.
Have you already tried this?
FileOutputStream fout = new FileOutputStream("YourPath", true);
The true value as the second parameter allows you to write in this file in append mode.
So you can serialize them individually and they will be serialized in the same file.
In Python i have a class with some string attributes and a function that returns an object of this class with atttributes set (sometimes can return an array of objects).
Theres any way to get this return in Java? Where i can see the strings of the object?
I Tried Jython but couldnt make it work!
Use json.dump function in Python to serialize your object into json format. Then use something like json.org library in Java to parse this object into Java object, some example over here.
Mind that not every object might be serializable, in general data structures like dictionaries or lists are easily serializable, from your description it seems like you want to move an instance of an object from one program into another, which is not possible to be done automatically and requires human work in rewriting the code as instances of classes contain not only data but also functions (methods).
Good luck!
Earlier I was storing only string in my file which can be store in SD card now I would to store byte[] also in same files. So do I just need to store normal to the file like this:
for string:- bufferWritter.write(data);
for bytes:- FileUtils.writeByteArrayToFile(new File("pathname"), myByteArray)
or fos.write(myByteArray);
So if I do like that then how can I differ message whether it is string or byte[].
Even I would like to know that is this is a good way to do?
You would need to write some sort of header to your file which should state what it does represent.
If you have multiple items in one file, also specify the length of the data parts.
[byte] sort of data (0=String, 1=Image)
And then the actual data.
But I would recommend you use a different format like json or make a serializable object.
I would give a try to some JSON implementation (maybe GSON? or some alternative) so you can have stored mixed data types in one file or write your own de/serialization routine so you can store whole objects.
Note: if you implement Serializable interface by a class that represent object to be stored, don't forget to re-generate UUID each time you change contents of that class, it will save you some time figuring out what went wrong
Someone told me that we use serialization only to convert object to binary? How far is that correct?
It is correct 😁
Class to be serialized needs to implement Serializable or Externalizable
You may then use ObjectOutputStream to serialize and ObjectInputStream to deserialize. As these are IO streams they could be abstractions over a file, socket etc.
I don't think the point is converting an object to binary. The only reason why anyone uses serialization is object storage - either in a file or in a database. The object can then be unserialized for later usage.
What are methods to convert data (ints, strings) to bytes in Java? I am looking for methods other than using the Serializable class. I researched and found things like ByteOutputStream.
Can I just parse strings and ints to a byte data type?
Any suggestions?
Have a look at DataInputStream and DataOutputStream, they convert all Java data types to bytes and read/write to an underlying Input/OutputStream.
If you need to read or write ints, longs etc.. to a file, then these are the classes for you.
If instead you are just interested in how to convert then to bytes for other purposes, have a look at the source code of those classes, they convert to big-endian.
Classes supporting the DataOutput interface will do what you want. Use DataInput to read the stream back to data.
The standard encoding method used by Java when serializing is, just as your own thoughts, a simple translation of the fields into a byte stream.
Primitives as well as non-transient, non-static referenced objects are encoded into the stream. Each object that is referenced by the serialized object and must also be serialized.
Other languages, such as PHP for example, serializes to a pretty much human readable format and some implementations serialize to JSON or XML.
In my own mind though, true serialization should be binary byte-per-byte representation of the data. That way it's possible to quickly read all the data up into memory again and it can be executed as is.