Java-OutputStreamWriter and InputStreamReader

Java-OutputStreamWriter and InputStreamReader

OutputStreamWriter

An OutputStreamWriter is a bridge from character streams to byte streams: Characters written to it are encoded into bytes using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform’s default charset may be accepted. Each invocation of a write() method causes the encoding converter to be invoked on the given character(s). The resulting bytes are accumulated in a buffer before being written to the underlying output stream. The size of this buffer may be specified, but by default it is large enough for most purposes. Note that the characters passed to the write() methods are not buffered.

OutputStreamWriter Constructors

Constructor Description
OutputStreamWriter(OutputStream out) Creates an OutputStreamWriter that uses the default character encoding.
OutputStreamWriter(OutputStream out, String charsetName) Creates an OutputStreamWriter that uses the named charset.
OutputStreamWriter(OutputStream out, Charset cs) Creates an OutputStreamWriter that uses the given charset.
OutputStreamWriter(OutputStream out, CharsetEncoder enc) Creates an OutputStreamWriter that uses the given charset encoder.

OutputStreamWriter Methods

Method Description
void write(int c) Writes a character(int to char) to the OutputStream.
void write(char[] c) Writes an array of characters to the OutputStream.
void write(char[] c, int off, int len) Writes a portion of an array of characters to the OutputStream.
void write(String str) Write a string to the OutputStream.
void write(String str, int off, int len) Write a portion of a string to the OutputStream.
CharArrayWriter append(char c) Appends the specified character to the OutputStream.
CharArrayWriter append(CharSequence csq) Appends the specified character sequence to the OutputStream.
CharArrayWriter append(CharSequence csq, int start, int end) Appends a subsequence of the specified character sequence to the OutputStream.
String  getEncoding() Returns the name of the character encoding being used by this stream.
void     flush() Flushes the stream.
void     close() Closes the stream, flushing it first.

InputStreamReader

An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform’s default charset may be accepted. Each invocation of one of an InputStreamReader’s read() methods may cause one or more bytes to be read from the underlying byte-input stream. To enable the efficient conversion of bytes to characters, more bytes may be read ahead from the underlying stream than are necessary to satisfy the current read operation.

InputStreamReader Constructors

Constructor Description
InputStreamReader(InputStream in) Creates an InputStreamReader that uses the default charset.
InputStreamReader(InputStream in, String charsetName) Creates an InputStreamReader that uses the named charset.
InputStreamReader(InputStream in, Charset cs) Creates an InputStreamReader that uses the given charset.
InputStreamReader(InputStream in, CharsetDecoder dec) Creates an InputStreamReader that uses the given charset decoder.

InputStreamReader Methods

Method Description
int read() Reads a single character.
int read(char[ ] cb) Reads up to cb.length characters from this stream into an array of characters.
int read(char[ ] cb, int off, int len) Reads up to len characters from this stream into an array of characters.
int read(CharBuffer cb) Attempts to read characters into the specified character buffer.
String     getEncoding() Returns the name of the character encoding being used by this stream.
boolean  markSupported() Tells whether this stream supports the mark() operation, which it does.
void        mark(int readAheadLimit) Sets the current marked position in the stream. The parameter gives the maximum limit of bytes that can be read before the marked position becomes invalid.
void        reset() Resets the stream to the most recent mark, or to the beginning if it has never been marked.
boolean  ready() Tells whether this stream is ready to be read.
long        skip(long n) Skips characters.
void        close() Closes the stream and releases any system resources associated with it.

Program

final1

Program Source

import java.io.OutputStreamWriter;
import java.io.InputStreamReader;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;

class Javaapp {
    
    public static void main(String[] args) throws IOException {
        
        FileInputStream fi = new FileInputStream("old.jpg");
        FileOutputStream fo = new FileOutputStream("encode.html");
        OutputStreamWriter osr = new OutputStreamWriter(fo);
        System.out.println(osr.getEncoding());
        
        int i;
        while((i=fi.read())!=-1)
        {
            osr.write(i);
        }
        fi.close(); 
        osr.close();
        
        FileOutputStream fo2 = new FileOutputStream("new.jpg");
        FileInputStream fi2 = new FileInputStream("encode.html");
        InputStreamReader isr = new InputStreamReader(fi2);
        System.out.println(isr.getEncoding());
        
        while((i=isr.read())!=-1)
        {
            fo2.write(i);
        }
        fo2.close(); 
        isr.close();
    }
}

Leave a Comment