OutputStreamWriter getEncoding() in Java

getEncoding(): This method is available in the java.io.OutputStreamWriter class of Java.

Syntax:

String java.io.OutputStreamWriter.getEncoding()

This method returns the name of the character encoding being used by this stream.

If the encoding has a historical name then that name is returned; otherwise, the encoding's canonical name is returned.

This method may return null if the stream has been closed.

Parameters: NA

Returns: The historical n.

Exceptions: NA

Approach

Java

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class OutputStreamWritergetEncoding {
    public static void main(String[] args) throws IOException {

        OutputStream out = new FileOutputStream("D:\\hello.txt");
        OutputStreamWriter outputStreamWriter =
new OutputStreamWriter(out);

        System.out.println(outputStreamWriter.getEncoding());
        outputStreamWriter.close();
    }
}

Output:

Cp1252


Some other methods of OutputStreamWriter

close(): This method closes the stream, flushing it first.

flush(): This method flushes the stream.

getEncoding(): This method returns the name of the character encoding being used by this stream.

OutputStreamWriter(OutputStream): This method creates an OutputStreamWriter that uses the default character encoding.

OutputStreamWriter(OutputStream, Charset): This method creates an OutputStreamWriter that uses the given charset.

OutputStreamWriter(OutputStream, CharsetEncoder): This method creates an OutputStreamWriter that uses the given charset encoder.

OutputStreamWriter(OutputStream, String): This method creates an OutputStreamWriter that uses the named charset.

write(int): This method writes a single character.

write(char[], int, int): This method writes a portion of an array of characters.

write(String, int, int): This method writes a portion of a string.

No comments:

Post a Comment