How to Write in the File using FileWriter class and BufferedWriter class in Core Java

In this tutorial how to write in the file using FileWriter class and BufferedWriter class in Core Java  is shown.

Code:

FileWriterDemo.java

import java.io.*;

class Test1
{
public static void main(String args[])
{
String fileName=”temp.txt”;
try
{
FileWriter fileWriter = new FileWriter(fileName);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(“Hello there,”);
bufferedWriter.write(” here is some text.”);
bufferedWriter.newLine();
bufferedWriter.write(“We are writing/reading”);
bufferedWriter.write(” the text to the file”);
bufferedWriter.close();
}
catch(IOException ex)
{
System.out.println(“Error writing to file ‘”+fileName+”‘”);
}
}
}

Add a Comment

Your email address will not be published. Required fields are marked *