
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java.io.PrintWriter.printf() Method
Description
The java.io.PrintWriter.printf() method is a convenience 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.
Declaration
Following is the declaration for java.io.PrintWriter.printf() method.
public PrintWriter printf(String format,Object... args)
Parameters
format − A format string as described in 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. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by the Java Virtual Machine Specification. The behaviour on a null argument depends on the conversion.
Return Value
This method returns this writer.
Exception
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.
NullPointerException − If the format is null.
Example
The following example shows the usage of java.io.PrintWriter.printf() method.
package com.tutorialspoint; import java.io.*; import java.util.Locale; public class PrintWriterDemo { public static void main(String[] args) { String s = "Hello World"; try { // create a new writer PrintWriter pw = new PrintWriter(System.out); // printf text with default locale. // %s indicates a string will be placed there, which is s pw.printf("This is a %s program", s); // change line pw.println(); // printf text with default locale // %d indicates a integer will be placed there, which is 100 pw.printf("This is a %s program with %d", s, 100); // flush the writer pw.flush(); } catch (Exception ex) { ex.printStackTrace(); } } }
Let us compile and run the above program, this will produce the following result −
This is a Hello World program This is a Hello World program with 100