Java – Write String to File

To write String to File in Java, there are many processes we can follow. Some of them are using FileUtils class of apache’s commons.io library, Input Streams, etc.

In this tutorial, we will learn some of the ways to write string to a file using following examples.

1. Read file as string using BufferedOutputStream

In this example, we will use BufferedOutputStream as the main step to write string data to file. Following is the sequence of steps.

  1. Create file object with the path of the text file.
  2. Create a FileOutputStream with the file object created in the above step.
  3. Using this FileOutputStream, create a BufferedOutputStream.
  4. Convert data string to byte array.
  5. Use BufferedOutputStream.write() to write the byte array to file.
  6. Close BufferedOutputStream and FileOutputStream to release any system resources associated with the streams.
  7. We have the file contents read to string.

WriteStringToFile.java

</>
Copy
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Java Example Program to Write String to File
 */

public class WriteStringToFile {

	public static void main(String[] args) {
		File file = new File("files/data1.txt");
		
		String data = "Hello World!\nWelcome to www.tutorialkart.com";
		
		try(FileOutputStream fos = new FileOutputStream(file);
				BufferedOutputStream bos = new BufferedOutputStream(fos)) {
			//convert string to byte array
			byte[] bytes = data.getBytes();
			//write byte array to file
			bos.write(bytes);
			bos.close();
			fos.close();
			System.out.print("Data written to file successfully.");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

FileOutputStream and BufferedOutputStream could throw IOException. Hence we used Java Try with Resource.

Run the program from command prompt or in your favorite IDE like Eclipse.

Output

Data written to file successfully.

2. Write string to file using commons.io

In this example, we shall use Apache’s commons.io package to write string to file.

  1. Create file object with the path to the text file.
  2. Have your data ready in a string.
  3. Call the method FileUtils.writeStringToFile() and pass the file object, data string as arguments to it. The function writes the data to file.

WriteStringToFile.java

</>
Copy
import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

/**
 * Java Example Program to Write String to File
 */

public class WriteStringToFile {

	public static void main(String[] args) {
		File file = new File("files/data1.txt");
		
		String data = "Hello World!\nWelcome to www.tutorialkart.com";
		
		try {
			//write string to file
			FileUtils.writeStringToFile(file, data);
			System.out.print("Data written to file successfully.");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}

Run this program, and you will get the following output.

Output

Data written to file successfully.

Conclusion

In this Java Tutorial, we learned how to write String to file, using inbuilt classes and some external Java packages.