Java.io.PrintWriter class in Java | Set 1
More methods:
- PrintWriter printf(Locale l, String format, Object… args) : A convenience method to write a formatted string to this writer using the specified format string and arguments.
Syntax :public PrintWriter printf(Locale l,
String format,
Object... args)
Parameters:
l - The locale to apply during formatting. If l is null then no localization is applied.
format - A format string as described in Format string syntax
args - Arguments referenced by the format specifiers in the format string.
Returns: This writer
Throws:
IllegalFormatException
NullPointerExceptionimport java.io.*;
import java.util.Locale;
public class PrintWriterDemo {
public static void main(String[] args) {
String s = "for";
PrintWriter pr= new PrintWriter(System.out);
pr.printf(Locale.CANADA, "Geeks%sGeeks", s);
pr.flush();
}
}
|
Output:
GeeksforGeeks
- PrintWriter printf(String format, Object… args) : A convenient method to write a formatted string to this writer using the specified format string and arguments.
Syntax :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.
Returns: This writer
Throws:
IllegalFormatException
NullPointerExceptionimport java.io.*;
public class PrintWriterDemo {
public static void main(String[] args) {
String s = "for";
PrintWriter pr= new PrintWriter(System.out);
pr.printf("Geeks%sGeeks", s);
pr.flush();
}
}
|
Output:
GeeksforGeeks
- void println(): Terminates the current line by writing the line separator string.
Syntax :public void println()
import java.io.*;
public class PrintWriterDemo {
public static void main(String[] args) {
String s = "GeeksforGeeks";
PrintWriter pr= new PrintWriter(System.out);
pr.printf("%s", s);
pr.flush();
}
}
|
Output:
GeeksforGeeks
- void println(boolean x): Prints a boolean and then terminate the line.
Syntax :public void println(boolean x)
import java.io.*;
public class PrintWriterDemo {
public static void main(String[] args) {
PrintWriter pr= new PrintWriter(System.out);
pr.println(true);
pr.print("New Line");
pr.flush();
}
}
|
Output:
true
New Line
- void println(char x): Prints a character and then terminate the line.
Syntax :public void println(char x)
import java.io.*;
public class PrintWriterDemo {
public static void main(String[] args) {
char c = 'a';
PrintWriter pr= new PrintWriter(System.out);
pr.println(c);
pr.println('b');
pr.flush();
}
}
|
Output:
a
b
- void println(char[] x): Prints an array of characters and then terminate the line.
Syntax :public void println(char[] x)
import java.io.*;
public class PrintWriterDemo {
public static void main(String[] args) {
char[] c = {'a', 'b', 'c'};
PrintWriter pr= new PrintWriter(System.out);
pr.println(c);
pr.flush();
}
}
|
Output:
abc
- void println(double x): Prints a double and then terminate the line.
Syntax :public void println(double x)
import java.io.*;
public class PrintWriterDemo {
public static void main(String[] args) {
double c = 176.348;
PrintWriter pr= new PrintWriter(System.out);
pr.println(c);
pr.flush();
}
}
|
Output:
176.348
- void println(float x): Prints a float and then terminate the line.
Syntax :public void println(float x)
import java.io.*;
public class PrintWriterDemo {
public static void main(String[] args) {
float c = 5.76348f;
PrintWriter pr= new PrintWriter(System.out);
pr.println(c);
pr.flush();
}
}
|
Output:
5.76348
- void println(int x): Prints an integer and then terminate the line.
Syntax :public void println(boolean x)
import java.io.*;
public class PrintWriterDemo {
public static void main(String[] args) {
int c = 15;
PrintWriter pr= new PrintWriter(System.out);
pr.println(c);
pr.flush();
}
}
|
Output:
15
- void println(long x): Prints a long integer and then terminate the line.
Syntax :public void println(long x)
import java.io.*;
public class PrintWriterDemo {
public static void main(String[] args) {
long c = 1252344125l;
try {
PrintWriter pr= new PrintWriter(System.out);
pr.println(c);
pr.flush();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
|
Output:
1252344125
- void println(Object x) :Prints an Object and then terminate the line.
Syntax :public void println(Object x)
import java.io.*;
public class PrintWriterDemo {
public static void main(String[] args) {
Object c = 10;
PrintWriter pr= new PrintWriter(System.out);
pr.println(c);
pr.flush();
}
}
|
Output:
10
- void println(String x) : Prints a String and then terminate the line.
Syntax :public void println(boolean x)
import java.io.*;
public class PrintWriterDemo {
public static void main(String[] args) {
String c = "GeeksforGeeks";
PrintWriter pr= new PrintWriter(System.out);
pr.println(c);
pr.flush();
}
}
|
Output:
GeeksforGeeks
- protected void setError() : Sets the error state of the stream to true.
Syntax :public void println(String x)
import java.io.*;
public class PrintWriterDemo extends PrintWriter {
public PrintWriterDemo(OutputStream out) {
super(out);
}
public static void main(String[] args) {
char c[] = {70, 71, 72, 73, 74, 75, 76};
PrintWriterDemo pr= new PrintWriterDemo(System.out);
pr.write(c, 1, 3);
pr.flush();
pr.setError();
}
}
|
Output:
GHI
- void write(char [] buf, int off, int len) : Writes len char from the specified char array starting at offset off to this stream.
Syntax :public void write(char [] buf,
int off,
int len)
Overrides:
write in class FilterOutputStream
Parameters:
buf - A char array
off - Offset from which to start taking char
len - Number of char to writeimport java.io.*;
public class PrintWriterDemo {
public static void main(String[] args) {
char c[] = {70, 71, 72, 73, 74, 75, 76};
PrintWriter pr= new PrintWriter(System.out);
pr.write(c, 1, 3);
pr.flush();
}
}
|
Output:
GHI
- void write(int b) : Writes the specified char to this stream.
Syntax :public void write(int b)
Overrides:
write in class Writer
Parameters:
b - The char to be written
import java.io.*;
public class PrintWriterDemo {
public static void main(String[] args) {
int c = 70;
PrintWriter pr= new PrintWriter(System.out);
pr.write(c);
pr.flush();
}
}
|
Output:
F
- void write(String s) : Writes a string.
Syntax :public void write(String s)
Parameters:
s - String to be written
import java.io.*;
public class PrintWriterDemo {
public static void main(String[] args) {
String s = "Hello";
try {
PrintWriter pw = new PrintWriter(System.out);
pw.write(s);
pw.write(" World");
pw.flush();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
|
Hello World
- void write(String s, int off, int len) : Writes a portion of a string.
Syntax :public void write(String s,
int off,
int len)
Parameters:
s - A String
off - Offset from which to start writing characters
len - Number of characters to write
import java.io.*;
public class PrintWriterDemo {
public static void main(String[] args) {
String s = "Hello World";
try {
PrintWriter pw = new PrintWriter(System.out);
pw.write(s, 0, 5);
pw.write(s, 6, 5);
pw.flush();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
|
HelloWorld
This article is contributed by Nishant Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.rt