Why should i use Serialization instead of File I/O in java - java

In serialization mechanism,we are wrote the object into stream using objectinputstream and object outputstream.These objects passing across the network.In this mechanismusing a Object input/output stream.So Can i use File INPUT/OUTPUT Streams instead of calling serialization marker interface?.

I guess You are mixing up serialization and general I/O.
Serialization is a way to transform objects into byte sequences (and back, which is called Deserialization). This way, You can transmit serializable objects over the network and store them into files.
File input/output streams are for storing/reading any kind of data to/from files.

when you need to transfer your object on network, you need to serialized it. Below link might be useful for you.
http://java.sun.com/developer/technicalArticles/Programming/serialization/

File I/O and Serialization are two different things. File I/O is used to read/write a file. Serialization interface is used for binary interpretation of an object. So NO, you can't use File Streams for sending over network.(maybe there is some workaround for sending data over network using file streams, but its like trying to fly with a car)

First let's concentrate on the definition:
Serialization: It is the process of converting object state into a format that can be stored and reconstructed later in the same way.
Whereas in file I/O it can't be possible to store data-structure or object and reconstructed later in the same way.
That's why we use serialization or database query methods (like sql, mongodb).
JSON/XML can also be used for serialization using its parser.
Take an example of javascript (not java, but take it like language-agnostics):
var obj = { // it's an object in javascript (same like json)
a: "something",
b: 3,
c: "another"
};
Now if you try to use file i/o in this to save in a file (say abc.txt), it will be saved as a string
which means it can't be accessed later in other code by reading this file (abc.txt) like this:
// readThisFile();
// obj.a;
But if you use serialization (in javascript using JSON natively), you can read it from the file

Since streams are additive, you can do something like
FileOutputStream fos = new FileOutputStream("/some/file/to/write/to");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(someObject);
Not sure this is what you were asking, but it's hard to tell.

Serialization/Deserialization is used to read and write objects, which not only makes compressed data, which is unreadable but also is writes it in binary. The File I/O is used for reading and writing. It appears that you do not want to serialize, if you don't, well do not use it. Read and write your files in text.

In serialization mechanism,we write the object into s stream using
ObjectInputStream and ObjectOutputStream.
Ok
These objects are passed across the network.In this mechanism using a
ObjectInput/Output stream.
I am following you.
So can I use File Input/Output streams instead of calling
serialization marker interface?.
Here you lost me. Do you mean to send an object over the network or just to serialize it?
Of course you can use whichever Input/Output streams along with ObjectInput/ObjectOutput streams to serialize objects to different media.
For instance:
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("jedis.bin"));
out.writeObject(new Jedi("Luke"));
Would serialize the object into a file called jedis.bin
And the code
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
ObjectOputStream out = new ObjectOutputStream(byteStream);
out.writeObject(new Jedi("Luke"));
Would serialize the object into a memory array.
So, anything that is an output/input stream is subject of being used as the underlying stream used by ObjectInput/ObjectOutput streams.

Related

Get an Publisher<ByteBuffer> from InputStream

I just upgraded my mongo-db-java-driver and now the handy function GridFSBucket.uploadFromStream has gone. Therefore we now got a
GridFSUploadPublisher<ObjectId> uploadFromPublisher(String filename, Publisher<ByteBuffer> source);
Any ideas how to convert my InputStream into an Publisher<ByteBuffer>? Is there any utilfunction in the java driver or Reactor?
There is a util in spring framework to convert input stream to data buffer flux. And it's direct to map data buffer as byte buffer.
Flux<ByteBuffer> byteBufferFlux = DataBufferUtils.readByteChannel(() -> Channels.newChannel(inputStream), DefaultDataBufferFactory.sharedInstance, 4096).map(DataBuffer::asByteBuffer);
To just make it work one could read stream and store data in byte[] array which later can be wrapped with ByteBuffer.wrap(byte[]) method.
I've looked into older MongoDB driver's source code and found stream to be used as actual stream, so it was not sneakily converted to ByteBuffer under the hood back then.
It seems there is no method to stream the data as it comes, there is no other way than to load file into memory now. I can see how it can be a problem in many scenarios, but maybe I just can't understand something about this approach.

Is it recommended to serialize & deserialize objects that are stored in arrayList?

In my small bank application, users have to input some value (name, SSN, amount etc..) and they get stored in an arrayList. The arrayList size is dynamic.
But problem with this one is I loose all data once I terminate the application. That leads me to think about the implementation of writing and reading file (file I/O).
Now I also have come to know about something called serialization and deserialization, though I am not quite sure in what situation this need to be implemented.
Do I need it in my particular case or simply writing into and reading from file will be enough?
What serialization and deserialization has to do with file I/O?
[NOTE: I will give more info if necessary]
This is where a Database comes into picture. To start with, you can use MySQL DB - it' an excellent FREE Database for small to medium size business apps. Later, if you intend to deploy your app to production - with large number of users & advance features, and are ready to pay a price for it - you might consider other databases like Oracle etc.
Storing info to files ((De)Serialization) is not recommended for any practical application.
Serialization is a mechanism where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.
ArrayList already implements Serializable, so in your example you could write something like this:
ArrayList<String> al=new ArrayList<String>();
al.add("Jean");
al.add("Pierre");
al.add("John");
try{
FileOutputStream fos= new FileOutputStream("myfile.txt");
ObjectOutputStream oos= new ObjectOutputStream(fos);
oos.writeObject(al);
oos.close();
fos.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
Here we save the list al in the file myfile.txt.
To read the file and get your ArrayList back, you would use ObjectInputStream:
FileInputStream fis = new FileInputStream("myfile.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList<String> list = (ArrayList<String>) ois.readObject();
ois.close();
Serialization is required when you want to write instances of your own class to a file. In your case, you can create a java class to hold all the values about customer, then override hashCode() and equals(), and then write your object to file. http://www.tutorialspoint.com/java/java_serialization.htm
Also, if you want, you can store individual field in file as well as int or String.
Though I would suggest to use a database to store all this information. But it seems you are a student and still in learning phase. So, interacting with DB right away might not be a good approach as of now.
Yes, you can use arraylist for serialization and deserialization.
Whenever u want to write and read the object into file and from file
respectively then u need to be object should be serialized and object
write into the file in byte stream format.that means ur data will be secure in
stream.you can used serialization interface:-
To persist data for future use.
To send data to a remote computer using such client/server Java technologies as RMI or socket programming.
To "flatten" an object into array of bytes in memory.
To exchange data between applets and servlets.
To store user session in Web applications.
To activate/passivate enterprise java beans.
To send objects between the servers in a cluster.
and more............

Save LinkedList to file and Load List back to program

I have a stock control system being used and i have a LinkedList to hold stock objects, now i need to add a method to the program that will allow me to save the linked list to a file and load the list from the file.
public void loadStockData(String filename)
and
public void saveStockData()
How would I achieve this?
Make sure your objects support serialization, hint: implements Serializable. http://docs.oracle.com/javase/6/docs/api/java/io/Serializable.html
Then, use java.io.ObjectOutputStream to save and java.io.ObjectInputStream to read the list.
http://www.tutorialspoint.com/java/java_serialization.htm
You did not mention if the file should be human readable.
If not then serialize the LinkedList object itself to the file given that the object it contains is also Serializable. You can use ObjectOutputStream to do that. Provide a FileOutputStream to the ObjectOutputStream
If you need in some specific format then you need to write the logic and using FileOutputStream you can write the file.

Java serialization of very large objects

I'm facing the following issue: I would like to serialize a very large object (several hundreds of MB in memory) to a file.
As I understood that Kryo is one of the best serialization libraries out there, I've been using this to serialize my object:
OutputStream fOutStream = new FileOutputStream(ParamsProvider.STORAGE_HH_FILENAME);
Output out = new Output(fOutStream);
kryo.writeObject(out, data);
out.close();
fOutStream.close();
This generates an OutOfMemory exception: I guess that the entire object is first serialized in memory before being written to file.
Hence my question: is there a way / library to serialize an entire object while it is written to file per chunks?
I would like to avoid the workaround of decomposing the object in smaller elements before serializing as:
I would like the serialization implementation to be independent of the data object structure
My object contains multiple references to the same objects. I suspect that if I serialize those elements independently, the serializer will instantiate them as different objects (consuming even more memory)
UPDATE:
In the meanwhile I implemented a serialization approach where the different elements of the very large object are serialized one by one and this does indeed avoid the OutOfMemory exception.
However I'm afraid that with this approach I will loose advantage (in terms of memory footprint) of multiple references to a same object (i.e. those references will have their own instance of the object). Any idea on this?
New code snippet (where, for instance, field1 contains references to same objects than field2):
OutputStream fOutStream = new FileOutputStream(ParamsProvider.STORAGE_HH_FILENAME);
Output out = new Output(fOutStream);
kryo.writeObject(out, data.field1);
kryo.writeObject(out, data.field2);
(...)
kryo.writeObject(out, data.field50);
out.close();
fOutStream.close();
Any hint / help would be greatly appreciated!
Thanks,
Tom

Storing Serializable Objects in the Database

I'm writing an application which needs to write an object into database.
For simplicity, I want to serialize the object.
But ObjectOuputStream needed for the same purpose has only one constructor which takes any subclass of OutputStream as parameter.
What parameter should be passed to it?
You can pass a ByteArrayOutputStream and then store the resulting stream.toByteArray() in the database as blob.
Make sure you specify a serialVersionUID for the class, because otherwise you'll have hard time when you add/remove a field.
Also consider the xml version for object serialization - XMLEncoder, if you need a bit more human-readable data.
And ultimately, you may want to translate your object model to the relational model via an ORM framework. JPA (Hibernate/EclipseLink/OpenJPA) provide object-relational mapping so that you work with objects, but their fields and relations are persisted in a RDBMS.
Using ByteArrayOutputStream should be a simple enough way to convert to a byte[] (call toByteArray after you've flushed). Alternatively there is Blob.setBinaryStream (which actually returns an OutputStream).
You might also want to reconsider using the database as a database...
e.g. create ByteArrayOutputStream and pass it to ObjectOuputStream constructor
One thing to add to this. java serialization is a good, general use tool. however, it can be a bit verbose. you might want to try gzipping the serialized data. you can do this by putting a GZIP stream between the object stream and the byte stream. this will use a small amount of extra cpu, but that is often a worthy tradeoff to shipping the extra bytes over the network and shoving them in a db.

Categories