PrintWriter(File, String): This method is available in the java.io.PrintWriter class of Java.
Syntax:
java.io.PrintWriter.PrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException
This method takes two arguments. This method creates a new PrintWriter, without automatic line flushing, with the specified file and charset.
Parameters: Two parameters are required for this method.
file: The file to use as the destination of this writer. 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.PrintWriter;public class PrintWriterPrintWriter6 {public static void main(String[] args) throws IOException {File file = new File("D:\\hello.txt");String csn = "UTF-8";PrintWriter printWriter = new PrintWriter(file, csn);System.out.println(printWriter);printWriter.close();}}
Output:
java.io.PrintWriter@4361bd48
Approach 2: UnsupportedEncodingException
Java
package com.PrintWriter;import java.io.File;import java.io.IOException;import java.io.PrintWriter;public class PrintWriterPrintWriter6 {public static void main(String[] args) throws IOException {File file = new File("D:\\hello.txt");String csn = "UTF-8888";PrintWriter printWriter = new PrintWriter(file, csn);System.out.println(printWriter);printWriter.close();}}
Output:
Exception in thread "main" java.io.UnsupportedEncodingException: UTF-8888 at java.base/java.io.PrintWriter.toCharset(PrintWriter.java:89) at java.base/java.io.PrintWriter.<init>(PrintWriter.java:349) at com.PrintWriter.PrintWriterPrintWriter6.main(PrintWriterPrintWriter6.java:12)
Some other methods of PrintWriter
append(char): This method appends the specified character to this writer.
append(CharSequence): This method appends the specified character sequence to this writer.
append(CharSequence, int, int): This method appends a subsequence of the specified character sequence to this writer.
checkError(): This method flushes the stream if it's not closed and checks its error state.
close(): This method closes the stream and releases any system resources associated with it.
flush(): This method flushes the stream.
format(String, Object...): This method writes a formatted string to this writer using the specified format string and arguments.
format(Locale, String, Object...): This method writes a formatted string to this writer 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 writer using the specified format string and arguments.
printf(Locale, String, Object...): It is a convenient method to write a formatted string to this writer using the specified format string and arguments.
println(): This method terminates the current line by writing the line separator string.
PrintWriter(File): This method creates a new PrintWriter, without automatic line flushing, with the specified file.
PrintWriter(Writer, boolean): This method creates a new PrintWriter.
PrintWriter(OutputStream, boolean, Charset): This method creates a new PrintWriter from an existing OutputStream.
PrintWriter(OutputStream): This method creates a new PrintWriter, without automatic line flushing, from an existing OutputStream.
PrintWriter(String): This method creates a new PrintWriter, without automatic line flushing, with the specified file name.
PrintWriter(Writer): This method creates a new PrintWriter, without automatic line flushing.
PrintWriter(File, Charset): This method creates a new PrintWriter, without automatic line flushing, with the specified file and charset.
PrintWriter(File, String): This method creates a new PrintWriter, without automatic line flushing, with the specified file and charset.
PrintWriter(OutputStream, boolean): This method creates a new PrintWriter from an existing OutputStream.
PrintWriter(String, Charset): This method creates a new PrintWriter, without automatic line flushing, with the specified file name and charset.
PrintWriter(String, String): This method creates a new PrintWriter, without automatic line flushing, with the specified file name and charset.
write(char[]): This method writes an array of characters.
write(int): This method writes a single character.
write(String): This method writes a string
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