(+91) 113 551 1687 contact@syncsas.com New Delhi

Develop your own utility class to read data and export results in Java

Java has a concept of working with streams of data. You can say that a Java program reads sequences of bytes from an input stream (or writes into an output stream): byte after byte, character after character, primitive after primitive. Accordingly, Java defines various types of classes supporting streams, for example, InputStream or OutputStream. There are classes specifically meant for reading character streams such as Reader and Writer.

Before an application can use a data file, it must open the file. A Java application opens a file by creating an object and associating a stream of bytes with that object. Similarly, when you finish using a file, the program should close the file—that is, make it no longer available to your application.

Below is a list of very important java library classes related to Streams.

How do I write an object to a file and read it back?
Java is pretty amazing with lots of API and with Java 8 we are fully enabled with lots more APIs like Lambda, Method reference, Default methods, Better type interface, Repeating annotations, Method parameter reflections and lot more.

Sometime back I’ve written an article on How to Read JSON Object From File in Java. It was simple java read operation. But in this tutorial we are going to save and load data from file with simple Production Ready Java Utility.

We are not only saving simple object but we will create simple Java POJO of type CrunchifyCompany and going to save and retrieve object using GSON. You need below dependency in order for below program to run.

Put below dependency to your maven project. If you have Dynamic Web Project and want to convert it into Maven project then follow these steps.

In modern Java applications you typically use the java.nio.file API to read and write files. Files exist on permanent storage devices, such as hard disks, Zip disks, USB drives, reels or cassettes of magnetic tape, and compact discs .Computer files are the electronic equivalent of paper files often stored in file cabinets in offices.

Java will read all input as a stream of bytes. The InputStream class is the superclass of all classes representing an input stream of bytes.