Java Write to a File Example

In this article, we will see different ways of writing into a File using Java Programming language. Java FileWriter class in java is used to write character-oriented data to a file as this class is character-oriented class because of what it is used in file handling in java.

There are many ways to write into a file in Java as there are many classes and methods which can fulfill the goal as follows:

  1. Using writeString() method
  2. Using FileWriter Class
  3. Using BufferedWriter Class
  4. Using FileOutputStream Class

Method 1: Using writeString() method

This method is supported by Java version 11. This method can take four parameters. These are file path, character sequence, charset, and options. The first two parameters are mandatory for this method to write into a file. It writes the characters as the content of the file. It returns the file path and can throw four types of exceptions. It is better to use when the content of the file is short.

Example: It shows the use of the writeString() method that is under the Files class to write data into a file. Another class, Path, is used to assign the filename with a path where the content will be written. Files class has another method named readString() to read the content of any existing file that is used in the code to check the content is properly written in the file.

Java

import java.io.IOException;

import java.nio.file.Files;

import java.nio.file.Path;

public class GFG {

public static void main(String[] args)

throws IOException

{

String text

= "Welcome to geekforgeeks\nHappy Learning!" ;

Path fileName = Path.of(

"/Users/mayanksolanki/Desktop/demo.docx" );

Files.writeString(fileName, text);

String file_content = Files.readString(fileName);

System.out.println(file_content);

}

}

Output

Welcome to geekforgeeks Happy Learning!

Method 2: Using FileWriter Class

If the content of the file is short, then using the FileWriter class to write in the file is another better option. It also writes the stream of characters as the content of the file like writeString() method. The constructor of this class defines the default character encoding and the default buffer size in bytes.

The following below example illustrates the use of the FileWriter class to write content into a file. It requires creating the object of the FileWriter class with the filename to write into a file. Next, the write() method is used to write the value of the text variable in the file. If any error occurs at the time of writing the file, then an IOException will be thrown, and the error message will be printed from the catch block.

Example:

Java

import java.io.FileWriter;

import java.io.IOException;

public class GFG {

public static void main(String[] args)

{

String text

= "Computer Science Portal GeeksforGeeks" ;

try {

FileWriter fWriter = new FileWriter(

"/Users/mayanksolanki/Desktop/demo.docx" );

fWriter.write(text);

System.out.println(text);

fWriter.close();

System.out.println(

"File is created successfully with the content." );

}

catch (IOException e) {

System.out.print(e.getMessage());

}

}

}

Output

File is created successfully with the content.

Method 3: Using BufferedWriter Class

It is used to write text to a character-output stream. It has a default buffer size, but a large buffer size can be assigned. It is useful for writing characters, strings, and arrays. It is better to wrap this class with any writer class for writing data to a file if no prompt output is required.

Example:

Java

import java.io.BufferedWriter;

import java.io.FileWriter;

import java.io.IOException;

public class GFG {

public static void main(String[] args)

{

String text

= "Computer Science Portal GeeksforGeks" ;

try {

BufferedWriter f_writer

= new BufferedWriter( new FileWriter(

"/Users/mayanksolanki/Desktop/demo.docx" ));

f_writer.write(text);

System.out.print(text);

System.out.print(

"File is created successfully with the content." );

f_writer.close();

}

catch (IOException e) {

System.out.print(e.getMessage());

}

}

}

Output

File is created successfully with the content.

The following example shows the use of BufferedWriter class to write into a file. It also requires creating the object of BufferedWriter class like FileWriter to write content into the file. But this class supports large content to write into the file by using a large buffer size.

Method 4: Using FileOutputStream Class

It is used to write raw stream data to a file. FileWriter and BufferedWriter classes are used to write only the text to a file, but the binary data can be written by using the FileOutputStream class.

To write data into a file using FileOutputStream class is shown in the following example. It also requires creating the object of the class with the filename to write data into a file. Here, the string content is converted into the byte array that is written into the file by using the write() method.

Example:

Java

import java.io.FileOutputStream;

import java.io.IOException;

public class GFG {

public static void main(String[] args)

{

String fileContent = "Welcome to geeksforgeeks" ;

FileOutputStream outputStream = null ;

try {

outputStream = new FileOutputStream( "file.txt" );

byte [] strToBytes = fileContent.getBytes();

outputStream.write(strToBytes);

System.out.print(

"File is created successfully with the content." );

}

catch (IOException e) {

System.out.print(e.getMessage());

}

finally {

if (outputStream != null ) {

try {

outputStream.close();

}

catch (IOException e) {

System.out.print(e.getMessage());

}

}

}

}

}

Output

File is created successfully with the content.

hudsonbrid1981.blogspot.com

Source: https://www.geeksforgeeks.org/java-program-to-write-into-a-file/

0 Response to "Java Write to a File Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel