All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Read And Write File In Java

Last Updated : Mar 11, 2024

Read And Write File In Java

In this article we will show you the solution of read and write file in java, when running the program, whether you're making a mobile app, a web app, or just writing scripts, you'll frequently have to read or write data into a file.

The data mentioned above could be cached data, data from the a dataset, a image, or whatever else you can conjure up.

Before we get into some actual examples, it's a good idea to understand the classes that can handle data reading and writing to files.

In the sections that follow, we'll go over the Java I/O classes and describe what they do, and we'll take a glance at Java NIO Streams, and at last we'll look at some illustrations of data reading and writing to files.

Character Streams are employed to read and write the data type characters Let's examine some of the most well-liked courses. These classes are all defined in the java.io package.

Here are a few classes that you should be familiar with if you want to read personality data:

  • Reader: A generic class for reading a character stream.
  • This class, known as InputStreamReader, reads byte streams and turns them into character streams.
  • FileReader: A class that allows you to read characters from a file.
  • BufferedReader: A Reader class wrapper that ostensibly supports buffering. In many cases, this is the best class to read data from because it can read more data from the file in a single read() call, minimizing the number of exact I/O operations with the file system.

A few classes for adding character data to files are listed below:

  • An abstract class to creating character streams is called Writer.
  • This class, known as OutputStreamWriter, writes character streams then transforms them into byte streams.
  • A class called FileWriter is employed to write characters into files.

Buffering is supported via this Writer class wrapper, which is called BufferedWriter.

This is the best class for writing a file's information because it can write more data to the folder in a single write() call.

This, like the BufferedReader, reduces the total number of I/O operations with the file system.

Step By Step Guide On Read And Write File In Java :-

import java.io.*;
public class TalkersCode FileRead{
   public static void main(String args[]) throws IOException {
   FileInputStream in = null;
   FileOutputStream out = null;
   try {
      in = new FileInputStream("C:\\demo\\Input-File.txt");
      out = new FileOutputStream("C:\\demo\\Output-File.txt");
      int c;
      while ((c = in.read()) != -1) {
         out.write(c);
      }
      System.out.println("TalkersCode Reading and Writing has been success!!!");
   }
   catch(Exception e){
           System.out.println(e);
   }finally {
      if (in != null) {
         in.close();
      }
      if (out != null) {
         out.close();
      }
   }
   }
}
  1. First, we start creating an import function called java.io.*
  2. The following step is to create a class named TalkersCode FileRead.
  3. As a result, as a string argument, a public static void main has been created.
  4. For the program's creation, we then throw an exception regarding the file input stream.
  5. We then generate an integer number.
  6. Finally, we write the file's name.
  7. Finally, we use system.out.println to exit the programme.

Conclusion :-

Java has offered a number of APIs (sometimes known at Java I/O) since its early releases, allowing users to R or W files.

In order to accommodate new features in later editions, Java I/O has indeed been improved, streamlined, and improved.

I hope this article on read and write file in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪