Writing content to a file using FileWriter class in java


FileWriter can be used to write a character file, It can be used to write a char or String in java


import java.io.FileWriter;
import java.io.IOException;

public class FileWriterExample {

	public static void main(String[] args) {

		try {
			FileWriter fw=new FileWriter("sam.txt");
			fw.write("Sample content to write in a file"); // writing a string to a file
			fw.write("\n"); // create a new line
			fw.write("second line of a sam file");
			fw.flush(); // flush the content before close
		} catch (IOException e) {
			// TODO Auto-generated catch block


Run the above program and try refreshing the directory or workspace to find the changes

Related Post