package com.java2novice.files;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class WriteToFile {
public static void main(String[] args) {
BufferedWriter bufferedWriter = null;
try {
String strContent = "This example shows how to save a string content to a text file";
File myFile = new File("C:/MyTestFile.txt");
// check if file exist, otherwise create the file before writing
if (!myFile.exists()) {
myFile.createNewFile();
}
Writer writer = new FileWriter(myFile);
bufferedWriter = new BufferedWriter(writer);
bufferedWriter.write(strContent);
} catch (IOException e) {
e.printStackTrace();
} finally{
try{
if(bufferedWriter != null) bufferedWriter.close();
} catch(Exception ex){
}
}
}
}