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

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

Syntax:

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

This method takes three arguments. It is a convenient method to write a formatted string to this output stream using the specified format string and arguments.

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 output stream.

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. For specification of all possible formatting errors, see the Details section of the formatter class specification.

2. NullPointerException - If the format is null.

Approach 1: When no exception

Java

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

public class PrintStreamprintf2 {
    public static void main(String[] args)
throws FileNotFoundException {

        File file = new File("D:\\hello.txt");
        PrintStream printStream = new PrintStream(file);

        String format = "HELLO JAVA PROGRAM";
        printStream.printf(Locale.CANADA, format, 0);

        System.out.println("Printing done");
        printStream.close();
    }
}

Output:

Printing done


hello.txt

HELLO JAVA PROGRAM

Approach 2: NullPointerException 

Java

package com.PrintStream;

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

public class PrintStreamprintf2 {
    public static void main(String[] args)
throws FileNotFoundException {

        File file = new File("D:\\hello.txt");
        PrintStream printStream = new PrintStream(file);

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

        System.out.println("Printing done");
        printStream.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.PrintStream.format(PrintStream.java:1267) at java.base/java.io.PrintStream.printf(PrintStream.java:1157) at com.PrintStream.PrintStreamprintf2.main(PrintStreamprintf2.java:15)


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