Java BufferedWriter Example

Here we uses the BufferedWriter stream to write ten lines of text to file File1.txt ,the result is WriteBufFile.

WriteBufFile

import java.io.*;

public class WriteBufFile
{

public static void main(String[] args) throws
IOException
{
String text;
int i;

FileWriter out = new FileWriter(''File1.txt");
BufferedWriter outBuffer = new BufferedWriter(out);
for (i = 1; i < 11; i++)
{

text = ''Line " + i + " of text\n";
outBuffer.write(text);
}
outBuffer.close();
out.close();
}
}



In line 10, we create a FileWriter stream as previously. In line 11, we create a BufferedWriter stream with the default buffer size to wrap the FileWriter stream, out.

The remaining code is as before except that in line 14 we write to the buffer stream using the java.io.BufferedWriter.write method.

No comments:

Post a Comment