All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Read And Write A File In Java

Last Updated : Mar 11, 2024

How To Read And Write A File In Java

In this article we will show you the solution of how to read and write a file in java, the abstract class of reading character streams is called the reader. It uses the following essential techniques:

  • read() reads one character at a time.
  • reads a character array using the function char[].
  • Skips a few characters when used with long.
  • Closes the stream with close().

A bridge between byte streams into character streams is provided by the inputstream reader.

It applies a certain charset when converting data to characters. When constructing an InputStreamReader, the charset can either be left as the operating system's default character encoding or specifically supplied.

FileReader is an easy-to-use class for reading text documents with the operating system's default character encoding.

BufferedReader is a straightforward way to read a line of text readLine and efficiently reads text from the a character stream (characters were buffered to prevent frequently reading from of the underlying stream) ().

The abstract class of creating character streams is called Writer. It uses the following essential techniques:

Writes one single character using the int syntax.

write(char[]): Writes a character array.

write(String) generates a string.

Closes the stream with close().

A bridge connecting character streams and byte streams is called OutputStreamWriter.

Bytes are converted from characters using a defined charset. When constructing an OutputStreamWriter, the charset can either be the operating system's default character encoding or specifically provided.

The practical class FileWriter makes it easy to create text files with the operating system's default character encoding.

In addition to offering an easy way to write a line separator, BufferedWriter efficiently writes text to the a character stream (characters, arrays, or strings were buffered to prevent frequently writing to a underlying stream)().

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

import java.io.File;
import java.io.IOException;
 public class TalkersCodeFile{
  public static void main(String[] args){
 try{
   File myObj = new File ("file.txt");
   if (myObj.createNewFile()) {
        System.out.println("File created: " + myObj.getName());
   } else {
        System.out.println("File already exists.");
   }
 } catch (IOException e) {
      System.out.println("An error occurred.");
      e.printStackTrace();
 }
  }
}
  1. First we create an import function with the name java.io.File
  2. Then we create a class with the name of TalkersCodeFile.
  3. Then we create a public static void main as string args.
  4. After that we create an try function for defining myobj file.
  5. Then we define if else statement for file creation.
  6. After that we close the program using system.out.println.

Conclusion :-

Using this technique, text is read from a character input stream. For rapid line, array, and character reading, it makes use of a buffer.

You can either specify the buffer size or use the default size. The default is sufficient for the majority of uses.

The underlying character and byte stream generally receives a corresponding used the for every reading request made of a Reader.

I hope this article on how to read and write a 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 🡪