Download java.io.BufferedWriter Class

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
http://www.hudatutorials.com
Download link: http://www.hudatutorials.com/ht/java/io/bufferedwriter.html
java.io.BufferedWriter Class
A BufferedWriter is a Writer that buffers output, writes text to a character-output
stream, buffering characters so as to provide for the efficient writing of single characters,
arrays, and strings. Using a BufferedWriter can improve performance by reducing the
number of times data is actually physically written to the output device.
Writer sends its output immediately to the underlying character or byte stream. Unless
prompt output is required, it is advisable to wrap a BufferedWriter around any Writer
whose write() operations may be costly, such as FileWriters and OutputStreamWriters.
A newLine() method is provided, which uses the platform's own notion of line separator
as defined by the system property line.separator. Not all platforms use the newline
character ('\n') to terminate lines. Calling this method to terminate each output line is
therefore preferred to writing a newline character directly.
A BufferedWriter has these two constructors:
BufferedWriter(Writer out)
Creates a buffered character-output stream that uses a default-sized output buffer.
BufferedWriter(Writer out, int sz)
Creates a new buffered character-output stream that uses an output buffer of the given
size.
The following program shows how to send output to console using java.
/* Following example shows how to write String to console. */
/* java.io.BufferedWriter class Example */
/* Save with file name BufferedWriterExample.java */
import java.io.*;
public class BufferedWriterExample
{
public static void main(String args[])
{
//java.io.BufferedWriter DECLARATION
java.io.BufferedWriter bw = null;
//OutputStreamWriter DECLARATION
OutputStreamWriter osw = null;
/*WE SHOULD USE try-catch BECAUSE MOST OF THE
JAVA I/O CLASES THROWS IOException */
try
{
String str = "This is test String for BufferedWriter.";
osw = new OutputStreamWriter(System.out);
bw = new BufferedWriter(osw);
bw.write(str,0,str.length());
1
http://www.hudatutorials.com
Download link: http://www.hudatutorials.com/ht/java/io/bufferedwriter.html
}
catch(Exception e)
{
System.out.println("Error Occurred : "+e.getMessage());
}
finally
{
//SHOULD CLOSE STREAMS FINALLY
try
{
if(bw!=null)
bw.close();
if(osw!=null)
osw.close();
}
catch(Exception ex)
{
System.out.println("Error Occurred : "+ex.getMessage());
}
}
}
}
/* Following example shows how to write String to console. */
/* java.io.BufferedWriter class Example 2*/
/* Save with file name BufferedWriterExample2.java */
import java.io.*;
public class BufferedWriterExample2
{
public static void main(String args[])
{
//java.io.BufferedWriter DECLARATION
java.io.BufferedWriter bw = null;
//OutputStreamWriter DECLARATION
OutputStreamWriter osw = null;
/*WE SHOULD USE try-catch BECAUSE MOST OF THE
JAVA I/O CLASES THROWS IOException */
try
{
String str = "This is test String 1 for BufferedWriter.";
String str2 = "This is test String 2 for newline method in BufferedWriter.";
osw = new OutputStreamWriter(System.out);
bw = new BufferedWriter(osw);
bw.write(str,0,str.length());
//WRITES NEW LINE CHARACTER TO OUTPUT STREAM
bw.newLine();
2
http://www.hudatutorials.com
Download link: http://www.hudatutorials.com/ht/java/io/bufferedwriter.html
bw.write(str2,0,str2.length());
}
catch(Exception e)
{
System.out.println("Error Occurred : "+e.getMessage());
}
finally
{
//SHOULD CLOSE STREAMS FINALLY
try
{
if(bw!=null)
bw.close();
if(osw!=null)
osw.close();
}
catch(Exception ex)
{
System.out.println("Error Occurred : "+ex.getMessage());
}
}
}
}
/* Following example shows how to read the text from file write to console. */
/* java.io.BufferedWriter class Example 3*/
/* Save with file name BufferedWriterExample3.java */
import java.io.*;
public class BufferedWriterExample3
{
public static void main(String args[])
{
//java.io.BufferedWriter DECLARATION
java.io.BufferedWriter bw = null;
//OutputStreamWriter DECLARATION
OutputStreamWriter osw = null;
//java.io.BufferedReader DECLARATION
java.io.BufferedReader br = null;
//InputStreamReader DECLARATION
InputStreamReader ir = null;
//FileInputStream DECLARATION
FileInputStream fis = null;
/*WE SHOULD USE try-catch BECAUSE MOST OF THE
JAVA I/O CLASES THROWS IOException */
try
{
int ch;
osw = new OutputStreamWriter(System.out);
3
http://www.hudatutorials.com
Download link: http://www.hudatutorials.com/ht/java/io/bufferedwriter.html
bw = new BufferedWriter(osw);
fis = new FileInputStream("BufferedWriterExample3.java");
ir = new InputStreamReader(fis);
br = new BufferedReader(ir);
while((ch = br.read()) != -1)
{
bw.write(ch);
}
}
catch(Exception e)
{
System.out.println("Error Occurred : "+e.getMessage());
}
finally
{
//SHOULD CLOSE STREAMS FINALLY
try
{
if(bw!=null)
bw.close();
if(osw!=null)
osw.close();
if(br!=null)
br.close();
if(ir!=null)
ir.close();
if(fis!=null)
fis.close();
}
catch(Exception ex)
{
System.out.println("Error Occurred : "+ex.getMessage());
}
}
}
}
/* Following example shows how to read total bytes from file into array and write to
console. */
/* java.io.BufferedWriter class Example 4*/
/* Save with file name BufferedWriterExample4.java */
import java.io.*;
public class BufferedWriterExample4
{
public static void main(String args[])
{
//java.io.BufferedWriter DECLARATION
java.io.BufferedWriter bw = null;
4
http://www.hudatutorials.com
Download link: http://www.hudatutorials.com/ht/java/io/bufferedwriter.html
//OutputStreamWriter DECLARATION
OutputStreamWriter osw = null;
//java.io.BufferedReader DECLARATION
java.io.BufferedReader br = null;
//InputStreamReader DECLARATION
InputStreamReader ir = null;
//FileInputStream DECLARATION
FileInputStream fis = null;
//ARRAY TO READ BYTES
char charbuff[] = null;
/*WE SHOULD USE try-catch BECAUSE MOST OF THE
JAVA I/O CLASES THROWS IOException */
try
{
int ch;
osw = new OutputStreamWriter(System.out);
bw = new BufferedWriter(osw);
fis = new FileInputStream("BufferedWriterExample4.java");
ir = new InputStreamReader(fis);
br = new BufferedReader(ir);
//GET THE AVAILABLE BYTES FROM InputReader AND CREATES ARRAY
charbuff = new char[fis.available()];
//READ TOTAL BYTES FROM FILE
br.read(charbuff, 0,fis.available());
bw.write(charbuff, 0,charbuff.length);
}
catch(Exception e)
{
System.out.println("Error Occurred : "+e.getMessage());
}
finally
{
//SHOULD CLOSE STREAMS FINALLY
try
{
//RELEASE ARRAY MEMORY
charbuff = null;
if(bw!=null)
bw.close();
if(osw!=null)
osw.close();
if(br!=null)
br.close();
5
http://www.hudatutorials.com
Download link: http://www.hudatutorials.com/ht/java/io/bufferedwriter.html
if(ir!=null)
ir.close();
if(fis!=null)
fis.close();
}
catch(Exception ex)
{
System.out.println("Error Occurred : "+ex.getMessage());
}
}
}
}
6