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.

See Also  Learn how to create an interface in java

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.

0 0 votes
Article Rating

Related articles

Do you want to develop Android Apps in Java with 1 example?

Java is the language of choice as we can...

How to Create a website using Java

How to Create a website using Java Is it possible...

Learn how to create an interface in java

In this article, we will learn how to create...

Download Java and install JDK

Hello, friends, this is my first post for java...

Case Studies

Compass Music Platform

A clothing brand wanted to launch a new e-commerce website that would allow customers to browse and purchase their products online. We developed a...

NewsWeek Magazine

A clothing brand wanted to launch a new e-commerce website that would allow customers to browse and purchase their products online. We developed a...

Beauty & Makeup Shop

A clothing brand wanted to launch a new e-commerce website that would allow customers to browse and purchase their products online. We developed a...
0
Would love your thoughts, please comment.x
()
x