In this tutorials we are going to see how to write file using BufferedWriter in Java.
1.Create project called JavaMisc.
2.Create package called com.javatutorialscorner.io
Create java class called FileWriter under com.javatutorialscorner.io package.
FileWriter.java
package com.javatutorialscorner.io;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileWriter {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedWriter bw = null;
java.io.FileWriter fw = null;
String message = "This is test String to be written in File named JavaTutorialsCorner";
File file = null;
try {
file = new File("C:\\jtc\\javatutorialscorner.txt");
if (!file.exists()) {
file.createNewFile();
}
fw = new java.io.FileWriter(file.getAbsoluteFile());
bw = new BufferedWriter(fw);
bw.write(message);
bw.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
BufferedWriter is unlike FileOutputStream.It is character stream class. So, we can directly write String, Array, character data into file.
Now run the program see the file created at specified location with text given below.
This is test String to be written in File named JavaTutorialsCorner
0 comments:
Post a Comment