Write Text File in Java (2024) | TechGeekNext

Write Text File in Java (2024)

In this tutorial, we will demonstrate how to write in Text File in Java.

Why stream is important for reading and writing Text file in Java?
Ans:

For reading and writing of CSV or text file Stream is very important. Stream is basically a process sequence through which data can either be written in a file or can be read from a file. Stream is a tube from which data bytes are either flow from a program to a destination file or from a destination file to a program.

  1. In order to write characters to a file, we need an outputStream.
  2. In order to read characters from a file, we need an inputStream.
  3. For reading and writing the CSV files, Java has java.io package that has to be imported either for writing or reading. For writing in text files, FileWriter, BufferedWriter are used.

Two ways in Java to write in Text File

  1. Write using FileWriter
  2. Write using BufferedWriter - Using Java 7 or higher version

What is the difference between FileWriter and BufferedWriter in Java?
Ans:

FileWriter writes to files directly and should only be used when the number of writes is minimal. Whereas BufferedWriter is similar to FileWriter, but it writes data into File using an internal buffer. When a result, as the number of write operations increases, the number of real IO operations decreases, and performance improves.

Java - Write in Text File using FileWriter

What is FileWriter in Java?
Ans:

The java.io package's FileWriter class is used to write data to a file in character form. FileWriter is a character-streaming program. Consider utilising a FileOutputStream for writing raw bytes streams. If the output file does not exist, FileWriter creates it..

import java.io.FileWriter;
public class TextWriteExample {
    public static void main(String args[]){
        try{

            FileWriter fileWriter = new FileWriter("D:\\TestFile.txt");

            fileWriter.write("TechGeekNext - Text FileWriter Example.");

        }
        catch(Exception exc) {
         System.out.println(exc.printStackTrace());
        }
        finally {
          fileWriter.close();
        }
        System.out.println("File updated successfully!");
    }
}

Take a look at our Suggested Posts :

Java - Write in Text File using BufferedWriter

What is BufferedWriter in Java?
Ans:

The java.io package includes a BufferedWriter. BufferedWriter buffers characters as they are written to a character output stream, allowing single characters, arrays, and strings to be written fast.

import java.io.*;
public class WriterExample {
public static void main(String[] args) throws Exception {

// use try-with-resource to close the buffered writer automatically   
try (Writer writer = new BufferedWriter(new OutputStreamWriter(
              new FileOutputStream("TestFile.txt"), "utf-8"))) {
   writer.write("TechGeekNext - Text BufferedWriter Example");
}

System.out.println("File updated successfully!");

 }
}

Recommendation for Top Popular Post :