How to write content into file in Java using BufferedWriter with example
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
public class WriteFileExampleUsingBufferedWriter
{
public static void main( String[] args )
{
try
{
String fileContent = "Content Added on "+new Date();
File file = new File("d:\\db\\fileTest\\newfile.txt");
//first check if the file is already there, if no file found then create new file
if (!file.exists())
{
file.createNewFile();
System.out.println("File Created Succefssfully");
}
FileWriter fw = new FileWriter(file.getName());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(fileContent);
bw.close();
System.out.println("Content written Succefssfully");
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
No comments:
Post a Comment