PrintWriter printf(Locale, String, Object...) in Java

printf(Locale, String, Object...): This method is available in the java.io.PrintWriter class of java.

Syntax:

PrintWriter java.io.PrintWriter.printf(Locale l, String format, Object... args)

This method takes three arguments. It is a convenient method to write a formatted string to this writer using the specified format string and arguments. If automatic flushing is enabled, calls to this method will flush the output buffer.

Parameters: Three parameters are required for this method.

l: The locale to apply during formatting. If l is null then no localization is applied.

format: A format string as described in the Format string syntax.

args: Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero.

Returns: This writer.

Throws:

1. java.util.IllegalFormatException - If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions.

2. NullPointerException - If the format is null.

Approach 1: When no exception

Java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Locale;

public class PrintWriterprintf2 {
    public static void main(String[] args)
throws FileNotFoundException {
        File file = new File("D:\\hello.txt");
        PrintWriter printWriter = new PrintWriter(file);

        String format = "HELLO";
        printWriter.printf(Locale.CANADA, format, 0);

        System.out.println("Successfully prints");
        printWriter.close();
    }
}

Output:

Successfully prints


Approach 2: NullPointerException

Java

package com.PrintWriter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Locale;

public class PrintWriterprintf2 {
    public static void main(String[] args)
throws FileNotFoundException {
        File file = new File("D:\\hello.txt");
        PrintWriter printWriter = new PrintWriter(file);

        String format = null;
        printWriter.printf(Locale.CANADA, format, 0);

        System.out.println("Successfully prints");
        printWriter.close();
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.CharSequence.length()" because "this.text" is null at java.base/java.util.regex.Matcher.getTextLength(Matcher.java:1770) at java.base/java.util.regex.Matcher.reset(Matcher.java:416) at java.base/java.util.regex.Matcher.<init>(Matcher.java:253) at java.base/java.util.regex.Pattern.matcher(Pattern.java:1134) at java.base/java.util.Formatter.parse(Formatter.java:2700) at java.base/java.util.Formatter.format(Formatter.java:2655) at java.base/java.io.PrintWriter.format(PrintWriter.java:1050) at java.base/java.io.PrintWriter.printf(PrintWriter.java:941) at com.PrintWriter.PrintWriterprintf2.main(PrintWriterprintf2.java:14)


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