Java OutputStreamWriter
Java OutputStreamWriter is useful in encoding a character streams to byte streams. The bytes are encoded using the specified charset. It can use a user defined charset or system’s default charset. We must wrap OutputStreamWriter within a java BufferedWriter class, it is a good practice and it also improves program efficiency.This class has 4 constructors as follows,
- OutputStreamWriter(OutputStream out)
- OutputStreamWriter(OutputStream out, Charset cs)
- OutputStreamWriter(OutputStream out, CharsetEncoder csencode)
- OutputStreamWriter(OutputStream out, String charsetname)
It provides some good methods like,
- write(int c) :It is used to write a single character.
- write(String str, int off, int len) : It is used to write a portion of given string starting from offset value till the len number of characters.
- write(char[] cbuf, int off, int len) : It is used to write a character array starting from the offset till the len number of characters.
- close() : It is used to close the stream.
- flush() : It is used to flush the data stream.
- getEncoding() : It is used to retrieve the name of the character encoding being used by the stream.
Example:
package com.javaeschool.javaexamples.io;
import java.io.OutputStreamWriter;
import java.io.OutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class JavaOutputStreamWriterExample
{
public static void main(String args[])
{
OutputStreamWriter osWriter = null;
OutputStream outStream = null;
File outputFile = null;
String str1 = "Hello Java.";
String str2 = "Java IO is fun.";
try{
outputFile = new File("d:\\outsWriter.txt"); outStream = new FileOutputStream(outputFile); osWriter = new OutputStreamWriter(outStream);
System.out.println("Writing data in file..!!");
osWriter.write(str1); osWriter.append(str2);
System.out.println("Data successfully written in file..!!");
}
catch(IOException e)
{
System.out.println("Exception caught..!!");
e.printStackTrace();
}
finally
{
try
{//closing objects
System.out.println("Flushing object..!!");
outStream.flush(); osWriter.flush();
System.out.println("Closing object..!!");
outStream.close(); osWriter.close();
}
catch(IOException ioe)
{
System.out.println("IOException caught..!!");
ioe.printStackTrace();
}
}
}
}
|
Output:-
Writing data in file..!! Data successfully written in file..!! Flushing object..!! Closing object..!! |
Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts.
ReplyDeleteSalesforce Training in Chennai | Certification | Online Course | Salesforce Training in Bangalore | Certification | Online Course | Salesforce Training in Hyderabad | Certification | Online Course | Salesforce Training in Pune | Certification | Online Course | Salesforce Certification Online Training Courses
This blog is the general information for the feature. You got a good work for these blog.
ReplyDeleteangular js training in chennai
angular js training in omr
full stack training in chennai
full stack training in omr
php training in chennai
php training in omr
photoshop training in chennai
photoshop training in omr