Java Write File
In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it is being used to begin writing character-oriented data to a file in Java.
As there are numerous classes and methods in Java that can achieve the following objectives, there are numerous ways to write into a file:
- Utilizing the writeString() method
- Applying the FileWriter Class
- BufferedWriter Class Utilization
- Employing the FileOutputStream Class
Utilizing the writeString() method
Java 11 is compatible with this technique. Four parameters are available for this approach. The file path, character set, charset, and options fall under this category. In order for this method to write to a file, the very first two parameters are necessary. The contents of the file are written as characters. It can throw four different exception kinds and returns the file path. It is preferable to utilize when the file's content is brief.
Example
It demonstrates how to put data together into a file using the Files class'writeString() function. To associate the filename with the path where the information will be written, a different class called Path is used. The readString() function of the Files class can be used in code to read the content of any already-existing file and verify that it was properly written there.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class Demo {
public static void main(String[] args)
throws IOException
{
String text = "Hello world";
Path fileName = Path.of("/Users/sravani/Desktop/demo.docx");
Files.writeString(fileName, text);
String file_content = Files.readString(fileName);
System.out.println(file_content);
}
}
Output
Hello world
Applying the FileWriter Class
Another preferable choice is to write into the file utilisingFileWriter class if the file's content is brief. Like the writeString() method, it also writes the character stream as the file's content. The default characters encoding as well as buffer size in bytes are set in the function Object() of this class.
The utilization of the FileWriter class for write material into a file is demonstrated in the example below. To write into a file, a FileWriter class object must be created and contain the filename. The content of the string variable is then written to the file using the write() method. An IOException will be thrown and the error message displayed from the catch block if any errors are made when writing the file.
Example
import java.io.FileWriter;
import java.io.IOException;
public class Demo {
public static void main(String[] args)
{
String text ="Hello world";
try {
FileWriterfWriter = new FileWriter("/Users/sravani/Desktop/demo.docx");
fWriter.write(text);
System.out.println(text);
fWriter.close();
System.out.println("File created successfully ");
}
catch (IOException e) {
System.out.print(e.getMessage());
}
}
}
Output
File created successfully
BufferedWriter Class Utilization
Text can be written to a character output stream using it. Although a huge buffer size can be provided, a default buffer size is present. It is helpful for writing strings, characters, and arrays. If there is no need for prompt output, it is preferable to wrap this class in any writer class when writing data to a file.
The use of the BufferedWriter class to write into a file is demonstrated in the following example. In order to write content into the file, an object of the BufferedWriter class, similar to FileWriter, must be created. However, this class makes use of a huge buffer size to facilitate writing enormous amounts of text into the file.
Example
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Demo {
public static void main(String[] args)
{
String text= "Hello world";
try {
BufferedWriterf_writer = new BufferedWriter(new FileWriter("/Users/sravani/Desktop/demo.docx"));
f_writer.write(text);
System.out.print(text);
System.out.print("File created successfully");
f_writer.close();
}
catch (IOException e) {
System.out.print(e.getMessage());
}
}
}
Output
File created successfully
Employing the FileOutputStream Class
Raw stream data is written using it in a file. The FileWriter, as well as BufferedWriter classes, only support writing text to files; the FileOutputStream class supports writing binary data. Use of the FileOutputStream class in the example that follows shows how and where to read or write into a file
To write data into a file, the class object with the filename must also be created. Here, using the write() method, the string content is transformed into a byte array and written to the file.
Example
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo {
public static void main(String[] args)
{
String fileContent = "Hello World";
FileOutputStreamoutputStream = null;
try {
outputStream = new FileOutputStream("file.txt");
byte[] strToBytes = fileContent.getBytes();
outputStream.write(strToBytes);
System.out.print(
"File created successfully ");
}
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 created successfully