Advertisement
OutputStreamWriter(OutputStream is) |
---|
This constructor creates an OutputStreamWriter object wrapped around an OutputStream to write data to this OutputStream in the form of bytes. Example-:
|
Methods | Description |
---|---|
flush() | This method flushes any data to be written out of this character output stream. |
write(int c) | This method writes one character at a time to this character output stream's buffer. |
write(char[]) | This method writes a whole character array at a time to this character output's buffer. |
write(String str) | This method writes a String to this character output stream's buffer. |
close() | This method closes this output stream and also frees any resources connected with this output stream. |
Advertisement
// Program to write a character array and a String using OutputStreamWriter.
import java.io.*;
class A
{
public static void main(String... ar)
{
String str="Hello World";
char[] arr= {'a', 'b', 'c' , 'd' , 'e'};
try
{
FileOutputStream fos= new FileOutputStream("D:\\TextBook.txt");
OutputStreamWriter osw= new OutputStreamWriter(fos);
// writing each character of character array using for-each loop
for(char c : arr)
{
osw.write(c);
}
//writing a String
osw.write(str);
osw.flush();
osw.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
}
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement