- Data file in java type
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
- Data file in java type
- Writing Java objects to files with ObjectOutputStream
- Reading Java objects from files with ObjectInputStream
- Advanced object serialization and deserialization topics
- Summary and outlook
Data file in java type
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter
Data file in java type
Java not only gives us the ability to write primitive data types and Strings. We can also write and read entire Java objects. For this purpose, Java provides the classes ObjectOutputStream and ObjectInputStream .
Writing Java objects to files with ObjectOutputStream
Using ObjectOutputStream.writeObject() , you can write any Java object into a file. The only prerequisite is that the object and all objects referenced by it – directly and transitively – are serializable (i.e. implement java.io.Serializable ). Otherwise, a NotSerializableException is thrown. Here is an example where we write a String , an ArrayList and a list created by List.of() into a file:
public class TestObjectOutputStream1 < public static void main(String[] args) throws IOException < try (var out = new ObjectOutputStream(new BufferedOutputStream( new FileOutputStream("objects1.bin")))) < // Write a string out.writeObject("Hello World äöü ß α € ↖ ?"); // Write an array list ArrayList list = new ArrayList(); list.add(42); list.add(47); list.add(74); out.writeObject(list); // Write an unmodifiable list out.writeObject(List.of("Hello", "World")); > > >
Code language: Java (java)
We see our String and can recognize a few class names, but more is not easily revealed. We will not discuss the binary format here.
Reading Java objects from files with ObjectInputStream
public class TestObjectInputStream1 < public static void main(String[] args) throws IOException, ClassNotFoundException < try (var fis = new FileInputStream("objects1.bin"); var in = new ObjectInputStream(new BufferedInputStream(fis))) < while (true) < Object o = in.readObject(); System.out.println("o.class hljs-string">"; o hljs-keyword">catch (EOFException ex) < System.out.println("EOF"); > > >
Code language: Java (java)
o.class = class java.lang.String; o = Hello World äöü ß α € ↖ ? o.class = class java.util.ArrayList; o = [42, 47, 74] o.class = class java.util.ImmutableCollections$List12; o = [Hello, World] EOF
Code language: plaintext (plaintext)
As you can see, we do not need to know the structure of the file, i.e., which object types it contains and in which order. ObjectOutputStream writes the respective class names into the file, and ObjectInputStream creates the corresponding objects again. There’s one particular characteristic we have to pay attention to with ObjectInputStream : In the try-with-resources block, it is essential to assign both FileInputStream and ObjectInputStream to one variable each. The following would be syntactically correct, but semantically wrong:
var out = new ObjectInputStream(new BufferedInputStream( new FileInputStream("objects1.bin")));
Code language: Java (java)
The reason is that ObjectInputStream ‘s constructor can throw an IOException . This happens if the binary file was not written by an ObjectOutputStream and, therefore, cannot be read by ObjectInputStream . In case of an exception, the (previously opened) FileInputStream would not be closed automatically, because only objects that are assigned to a variable in the try block are closed.
Advanced object serialization and deserialization topics
ObjectOutputStream and ObjectInputStream are much more powerful than shown here. Their purpose – the serialization and deserialization of Java objects – is not only used in the context of file operations. But also, for example, in distributed in-memory caches or remote method invocation. This article is not intended as a tutorial about Java object serialization and deserialization. So I will not go into details here (like «back references», writeUnshared() , readUnshared() , writeObject() , readObject() , etc.). I will write a detailed tutorial on these advanced serialization topics after the series about files is finished.
Summary and outlook
In this article, you have seen how to use DataOutputStream and DataInputStream to write primitive data types and Strings to and read them from files, and how to use ObjectOutputStream and ObjectInputStream to write and read complex Java objects. We have only scratched the surface of ObjectOutputStream and ObjectInputStream . I will cover advanced serialization topics in a future article. In the next and last article, I will introduce you to the FileChannel and ByteBuffer classes added in Java 1.4. These speed up working with huge files (when used with direct buffers), allow setting locks on file sections, and mapping files into memory («memory-mapped files») to access them as easily as byte arrays. If you liked this article, please take a moment to share it using one of the share buttons below. If you would like to be notified when the next part is published, click here to sign up for the HappyCoders newsletter.