In this section we will learn about the Java OutputStreamWriter.
In this section we will learn about the Java OutputStreamWriter.In this section we will learn about the Java OutputStreamWriter.
java.io.OutputStreamWriter is a character written output stream which encodes the characters into bytes, for encoding this class uses a specific charset. OutputStreamWriter is acted as a bridge into encoding of character streams to byte streams. write() method in OutputStreamWriter facilitate to write characters to the stream. Wrapping within BufferedWriter of this class makes it more efficient.
Constructor Detail
OutputStreamWriter(OutputStream out) | This constructor is used to create a new OutputStreamWriter contains the
characters stream. This constructor uses the default encoding
system from characters to bytes. Syntax : public OutputStreamWriter(OutputStream out) |
OutputStreamWriter(OutputStream out, Charset cs) | This constructor is used to create new OutputStreamWriter contains the
character stream. This constructor uses the specified charset. Syntax : public OutputStreamWriter(OutputStream out, Charset cs) |
OutputStreamWriter(OutputStream out, CharsetEncoder enc) | This constructor is used to create a new OutpuStreamWriter contains the
characters stream. This constructor uses a specified charset encoder. Syntax : public OutputStreamWriter(OutputStream out, CharsetEncoder enc) |
OutputStreamWriter(OutputStream out, String charsetName) | This constructor is used to create new OutputStreamWriter contains the
characters stream. This constructor uses a specified string charset. Syntax : public OutputStreamWriter(OutputStream out, String charsetName) throws UnsupportedEncodingException |
Method Detail
Example
Here an example is being given which will demonstrate about how to use the OutputStreamWriter. A Java class that I have created for demonstrating how to use the OutputStreamWriter. In this class I have used various classes of java.io package for completing this example. As it is discussed that OutputStreamWriter class should be wrapped within BufferedWriter so I have used this class in my example except this the FileOutputStream class is used. Using this example I have tried to write the data to a file. So, when you will executed this example successfully you will see that a file is created with the name (if not existed) and in the directory specified by you.
Source Code
JavaOutputStreamWriterExample.java
import java.io.BufferedWriter; import java.io.OutputStreamWriter; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.IOException; public class JavaOutputStreamWriterExample { public static void main(String args[]) { OutputStream os = null; OutputStreamWriter osw = null; BufferedWriter bw = null; try { /*os = new FileOutputStream("test.txt"); osw = new OutputStreamWriter(os); osw.write("Java IO OutputStreamWriter Example"); */ os = new FileOutputStream("test.txt"); osw = new OutputStreamWriter(os); bw = new BufferedWriter(osw); bw.write("Hello"); bw.newLine(); bw.write("Java IO OutputStreamWriter Example"); System.out.println("Contents written to the file successfully."); } catch(IOException ioe) { System.out.println(ioe); } finally { if(os != null) { try { bw.flush(); bw.close(); os.flush(); os.close(); osw.close(); } catch(Exception e) { System.out.println(e); } } }// finally close }// main close }// class close
Output
When you will compile and execute this example you will get the output as follows :
And to see where the contents has been written go to your directory what you have specified at the programming time and open your file.
Ads