Java BufferedWriter
BufferedWriter is the class which is under the package java.io.
BufferedWriter Write text to a character-output stream.
We can write characters to the file by using the write methods .
Below i am giving the very simple example, which writes the some text content
to the file. if file is there it overwrites the content, if file is not there, it creates one file
Java BufferedWriter Example
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
//Write the content into the file
public class Java_BufferedWriterExample {
public static void main(String[] args) {
BufferedWriter br = null;
FileWriter fr = null;
File file = null;
try {
file = new File("C:\\core1.txt");
file.createNewFile();
fr = new FileWriter(file);
br = new BufferedWriter(fr);
fr.write("hello");
fr.write("how are you");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fr != null){
fr.close();
fr = null;
}
if (br != null){
br.close();
br = null;
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
0 comments:
Post a Comment