PrintStream(File, String): This method is available in the java.io.PrintStream class of Java.
Syntax:
java.io.PrintStream.PrintStream(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException
This method takes two arguments. This method creates a new print stream, without automatic line flushing, with the specified file and charset.
This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the provided charset.
Parameters: Two parameters are required for this method.
file: The file to use as the destination of this print stream. If the file exists, then it will be truncated to zero sizes; otherwise, a new file will be created. The output will be written to the file and buffered.
csn: The name of a supported charset.
Throws:
1. FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file.
2. SecurityException - If a security manager is present and checkWrite(file.getPath())denies write access to the file.
3. UnsupportedEncodingException - If the named charset is not supported.
Approach 1: When no exception
Java
import java.io.File;import java.io.IOException;import java.io.PrintStream;public class PrintStreamPrintStream5 {public static void main(String[] args) throws IOException {File file = new File("D:\\hello.txt");String csn = "UTF-8";PrintStream printStream = new PrintStream(file, csn);System.out.println(printStream);printStream.close();}}
Output:
java.io.PrintStream@26f0a63f
Approach 2: UnsupportedEncodingException
Java
package com.PrintStream;import java.io.File;import java.io.IOException;import java.io.PrintStream;public class PrintStreamPrintStream5 {public static void main(String[] args) throws IOException {File file = new File("D:\\hello.txt");String csn = "csn";PrintStream printStream = new PrintStream(file, csn);System.out.println(printStream);printStream.close();}}
Output:
Exception in thread "main" java.io.UnsupportedEncodingException: csn at java.base/java.io.PrintStream.toCharset(PrintStream.java:102) at java.base/java.io.PrintStream.<init>(PrintStream.java:372) at com.PrintStream.PrintStreamPrintStream5.main(PrintStreamPrintStream5.java:12)
Some other methods of PrintStream
append(char): This method appends the specified character to this output stream.
append(CharSequence): This method appends the specified character sequence to this output stream.
append(CharSequence, int, int): This method appends a subsequence of the specified character sequence to this output stream.
checkError(): This method flushes the stream and checks its error state.
close(): This is done by flushing the stream and then closing the underlying output stream.
flush(): This method flushes the stream.
format(String, Object...): This method writes a formatted string to this output stream using the specified format string and arguments.
format(Locale, String, Object...): This method writes a formatted string to this output stream using the specified format string and arguments.
print(boolean): This method prints a boolean value.
printf(String, Object...): It is a convenient method to write a formatted string to this output stream using the specified format string and arguments.
printf(Locale, String, Object...): It is a convenient method to write a formatted string to this output stream using the specified format string and arguments.
println(): This method terminates the current line by writing the line separator string.
PrintStream(File): This method creates a new print stream, without automatic line flushing, with the specified file.
PrintStream(OutputStream, boolean, String): This method creates a new print stream, with the specified OutputStream, line flushing, and character encoding.
PrintStream(OutputStream): This method creates a new print stream, without automatic line flushing, with the specified OutputStream.
PrintStream(String): This method creates a new print stream, without automatic line flushing, with the specified file name.
PrintStream(File, Charset): This method creates a new print stream, without automatic line flushing, with the specified file and charset.
PrintStream(File, String): This method creates a new print stream, without automatic line flushing, with the specified file and charset.
PrintStream(OutputStream, boolean): This method creates a new print stream, with the specified OutputStream and line flushing.
PrintStream(String, Charset): This method creates a new print stream, without automatic line flushing, with the specified file name and charset.
PrintStream(String, String): This method creates a new print stream, without automatic line flushing, with the specified file name and charset.
PrintStream(OutputStream, boolean, Charset): This method creates a new print stream, with the specified OutputStream, line flushing, and charset.
write(byte[]): This method writes all bytes from the specified byte array to this stream.
write(int): This method writes the specified byte to this stream.
write(byte[], int, int): This method writes len bytes from the specified byte array starting to offset off to this stream.
writeBytes(byte[]): This method writes all bytes from the specified byte array to this stream.
No comments:
Post a Comment