Java : PrintWriter with Examples
PrintWriter (Java SE 22 & JDK 22) with Examples.
You will find code examples on most PrintWriter methods.
Summary
final var sw = new StringWriter();
try (final var writer = new PrintWriter(sw)) {
writer.println("abc");
writer.printf("num = %d", 1234);
}
System.out.println(sw);
// Result
// ↓
//abc
//num = 1234
Fields
protected Writer out
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
Fields declared in Writer
Constructors
PrintWriter (File file)
final var file = Path.of("R:", "java-work", "sample.txt");
try (final var writer = new PrintWriter(file.toFile())) {
writer.println("abc");
writer.println(1234);
writer.println("○△×");
}
System.out.print(Files.readString(file));
// Result
// ↓
//abc
//1234
//○△×
PrintWriter (File file, String csn)
System.out.println("default charset : " + Charset.defaultCharset());
final var file = Path.of("R:", "java-work", "sample.txt");
final var csn = "Shift_JIS";
System.out.println("-- write --");
try (final var writer = new PrintWriter(file.toFile(), csn)) {
writer.println("abc");
writer.println(1234);
writer.println("○△×");
}
System.out.print(Files.readString(file, Charset.forName(csn)));
// Result
// ↓
//default charset : UTF-8
//-- write --
//abc
//1234
//○△×
PrintWriter (File file, Charset charset)
System.out.println("default charset : " + Charset.defaultCharset());
final var file = Path.of("R:", "java-work", "sample.txt");
final var charset = Charset.forName("Shift_JIS");
System.out.println("-- write --");
try (final var writer = new PrintWriter(file.toFile(), charset)) {
writer.println("abc");
writer.println(1234);
writer.println("○△×");
}
System.out.print(Files.readString(file, charset));
// Result
// ↓
//default charset : UTF-8
//-- write --
//abc
//1234
//○△×
PrintWriter (OutputStream out)
final var out = new ByteArrayOutputStream();
try (final var writer = new PrintWriter(out)) {
writer.println("abc");
writer.println(1234);
}
System.out.println(out);
// Result
// ↓
//abc
//1234
PrintWriter (OutputStream out, boolean autoFlush)
// autoFlush = false
final var out = new ByteArrayOutputStream();
try (final var writer = new PrintWriter(out, false)) {
writer.print("abc");
System.out.println(out.toString().isEmpty());
// Result
// ↓
//true
writer.println("XYZ");
System.out.println(out.toString().isEmpty());
// Result
// ↓
//true
writer.printf("num = %d", 1234);
System.out.println(out.toString().isEmpty());
// Result
// ↓
//true
}
System.out.println(out);
// Result
// ↓
//abcXYZ
//num = 1234
// autoFlush = true
final var out = new ByteArrayOutputStream();
try (final var writer = new PrintWriter(out, true)) {
writer.print("abc");
System.out.println(out.toString().isEmpty());
// Result
// ↓
//true
writer.println("XYZ");
System.out.println(out);
// Result
// ↓
//abcXYZ
writer.printf("num = %d", 1234);
System.out.println(out);
// Result
// ↓
//abcXYZ
//num = 1234
}
System.out.println(out);
// Result
// ↓
//abcXYZ
//num = 1234
PrintWriter (OutputStream out, boolean autoFlush, Charset charset)
Please see also : PrintWriter(OutputStream out, boolean autoFlush)
System.out.println("default charset : " + Charset.defaultCharset());
final var out = new ByteArrayOutputStream();
final var charset = Charset.forName("Shift_JIS");
System.out.println("-- write --");
try (final var writer = new PrintWriter(out, false, charset)) {
writer.println("abc");
writer.println(1234);
writer.println("○△×");
}
System.out.print(out.toString(charset));
// Result
// ↓
//default charset : UTF-8
//-- write --
//abc
//1234
//○△×
PrintWriter (Writer out)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.println("abc");
writer.println(1234);
writer.println("○△×");
}
System.out.print(out);
// Result
// ↓
//abc
//1234
//○△×
PrintWriter (Writer out, boolean autoFlush)
// autoFlush = false
final var sw = new StringWriter();
try (final var out = new BufferedWriter(sw);
final var writer = new PrintWriter(out, false)) {
writer.print("abc");
System.out.println(sw.toString().isEmpty());
// Result
// ↓
//true
writer.println("XYZ");
System.out.println(sw.toString().isEmpty());
// Result
// ↓
//true
writer.printf("num = %d", 1234);
System.out.println(sw.toString().isEmpty());
// Result
// ↓
//true
}
System.out.println(sw);
// Result
// ↓
//abcXYZ
//num = 1234
// autoFlush = true
final var sw = new StringWriter();
try (final var out = new BufferedWriter(sw);
final var writer = new PrintWriter(out, true)) {
writer.print("abc");
System.out.println(sw.toString().isEmpty());
// Result
// ↓
//true
writer.println("XYZ");
System.out.println(sw);
// Result
// ↓
//abcXYZ
writer.printf("num = %d", 1234);
System.out.println(sw);
// Result
// ↓
//abcXYZ
//num = 1234
}
System.out.println(sw);
// Result
// ↓
//abcXYZ
//num = 1234
PrintWriter (String fileName)
final var file = Path.of("R:", "java-work", "sample.txt");
final var fileName = file.toString();
System.out.println("file name : " + fileName);
System.out.println("-- write --");
try (final var writer = new PrintWriter(fileName)) {
writer.println("abc");
writer.println(1234);
writer.println("○△×");
}
System.out.print(Files.readString(file));
// Result
// ↓
//file name : R:\java-work\sample.txt
//-- write --
//abc
//1234
//○△×
PrintWriter (String fileName, String csn)
System.out.println("default charset : " + Charset.defaultCharset());
final var file = Path.of("R:", "java-work", "sample.txt");
final var fileName = file.toString();
System.out.println("file name : " + fileName);
final var csn = "Shift_JIS";
System.out.println("-- write --");
try (final var writer = new PrintWriter(fileName, csn)) {
writer.println("abc");
writer.println(1234);
writer.println("○△×");
}
System.out.print(Files.readString(file, Charset.forName(csn)));
// Result
// ↓
//default charset : UTF-8
//file name : R:\java-work\sample.txt
//-- write --
//abc
//1234
//○△×
PrintWriter (String fileName, Charset charset)
System.out.println("default charset : " + Charset.defaultCharset());
final var file = Path.of("R:", "java-work", "sample.txt");
final var fileName = file.toString();
System.out.println("file name : " + fileName);
final var charset = Charset.forName("Shift_JIS");
System.out.println("-- write --");
try (final var writer = new PrintWriter(fileName, charset)) {
writer.println("abc");
writer.println(1234);
writer.println("○△×");
}
System.out.print(Files.readString(file, charset));
// Result
// ↓
//default charset : UTF-8
//file name : R:\java-work\sample.txt
//-- write --
//abc
//1234
//○△×
Methods
PrintWriter append (char c)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.append('a').append('b').append('c');
}
System.out.println(out); // abc
PrintWriter append (CharSequence csq)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.append("abc").append("-").append("XYZ");
}
System.out.println(out); // abc-XYZ
PrintWriter append (CharSequence csq, int start, int end)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
final var csq = "abcd";
writer.append(csq, 0, 1).println();
writer.append(csq, 0, 2).println();
writer.append(csq, 0, 3).println();
writer.append(csq, 0, 4).println();
}
System.out.print(out);
// Result
// ↓
//a
//ab
//abc
//abcd
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
final var csq = "abcd";
writer.append(csq, 0, 4).println();
writer.append(csq, 1, 4).println();
writer.append(csq, 2, 4).println();
writer.append(csq, 3, 4).println();
}
System.out.print(out);
// Result
// ↓
//abcd
//bcd
//cd
//d
boolean checkError ()
final var file = Path.of("R:", "java-work", "sample.txt");
try (final var out = Files.newBufferedWriter(file);
final var writer = new PrintWriter(out)) {
writer.println("abc");
System.out.println("error : " + writer.checkError());
// Intentionally close the out object to make an error.
System.out.println("-- close --");
out.close();
writer.println("XYZ");
System.out.println("error : " + writer.checkError());
}
// Result
// ↓
//error : false
//-- close --
//error : true
protected void clearError ()
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
void close ()
final var file = Path.of("R:", "java-work", "sample.txt");
try (final var writer = new PrintWriter(file.toFile())) {
writer.print("abc");
writer.print("XYZ");
}
System.out.println(Files.readString(file)); // abcXYZ
// An example without a try-with-resources statement.
final var file = Path.of("R:", "java-work", "sample.txt");
final var writer = new PrintWriter(file.toFile());
try {
writer.print("abc");
writer.print("XYZ");
} finally {
writer.close();
}
System.out.println(Files.readString(file)); // abcXYZ
void flush ()
final var out = new ByteArrayOutputStream();
try (final var writer = new PrintWriter(out, false)) {
writer.print("abc");
System.out.println(out.toString().isEmpty()); // true
writer.print(123);
System.out.println(out.toString().isEmpty()); // true
writer.flush();
System.out.println(out); // abc123
}
PrintWriter format (String format, Object... args)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.format("text = %s%n", "abc");
writer.format("num = %d%n", 1234);
writer.format("X").format("Y").format("Z");
}
System.out.println(out);
// Result
// ↓
//text = abc
//num = 1234
//XYZ
PrintWriter format (Locale l, String format, Object... args)
System.out.println("locale : " + Locale.getDefault().toLanguageTag());
System.out.println("-- write --");
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
final var time = LocalTime.of(14, 30);
writer.format("%tr%n", time);
writer.format(Locale.JAPANESE, "%tr%n", time);
}
System.out.print(out);
// Result
// ↓
//locale : en-US
//-- write --
//02:30:00 PM
//02:30:00 午後
void print (boolean b)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.print(true);
writer.print(" : ");
writer.print(false);
}
System.out.println(out); // true : false
void print (char c)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.print('a');
writer.print(" : ");
writer.print('b');
}
System.out.println(out); // a : b
void print (char[] s)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.print(new char[]{'a', 'b', 'c'});
writer.print(" : ");
writer.print(new char[]{'X', 'Y', 'Z'});
}
System.out.println(out); // abc : XYZ
void print (double d)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.print(1.234);
writer.print(" : ");
writer.print(7.89e+10);
}
System.out.println(out); // 1.234 : 7.89E10
void print (float f)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.print(1.234f);
writer.print(" : ");
writer.print(7.89e+10f);
}
System.out.println(out); // 1.234 : 7.89E10
void print (int i)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.print(1234);
writer.print(" : ");
writer.print(Integer.MAX_VALUE);
}
System.out.println(out); // 1234 : 2147483647
void print (long l)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.print(1234L);
writer.print(" : ");
writer.print(Long.MAX_VALUE);
}
System.out.println(out); // 1234 : 9223372036854775807
void print (Object obj)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.print(Path.of("R:", "java-work"));
writer.print(" : ");
writer.print(LocalTime.of(14, 30));
}
System.out.println(out); // R:\java-work : 14:30
void print (String s)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.print("abc");
writer.print(" : ");
writer.print("XYZ");
}
System.out.println(out); // abc : XYZ
PrintWriter printf (String format, Object... args)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.printf("text = %s%n", "abc");
writer.printf("num = %d%n", 1234);
writer.printf("X").printf("Y").printf("Z");
}
System.out.println(out);
// Result
// ↓
//text = abc
//num = 1234
//XYZ
PrintWriter printf (Locale l, String format, Object... args)
System.out.println("locale : " + Locale.getDefault().toLanguageTag());
System.out.println("-- write --");
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
final var time = LocalTime.of(14, 30);
writer.printf("%tr%n", time);
writer.printf(Locale.JAPANESE, "%tr%n", time);
}
System.out.print(out);
// Result
// ↓
//locale : en-US
//-- write --
//02:30:00 PM
//02:30:00 午後
void println ()
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.print("abc");
writer.println();
writer.print(123);
}
System.out.println(out);
// Result
// ↓
//abc
//123
void println (boolean x)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.println(true);
writer.println(false);
}
System.out.print(out);
// Result
// ↓
//true
//false
void println (char x)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.println('a');
writer.println('b');
}
System.out.print(out);
// Result
// ↓
//a
//b
void println (char[] x)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.println(new char[]{'a', 'b', 'c'});
writer.println(new char[]{'X', 'Y', 'Z'});
}
System.out.print(out);
// Result
// ↓
//abc
//XYZ
void println (double x)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.println(1.234);
writer.println(7.89e+10);
}
System.out.print(out);
// Result
// ↓
//1.234
//7.89E10
void println (float x)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.println(1.234f);
writer.println(7.89e+10f);
}
System.out.print(out);
// Result
// ↓
//1.234
//7.89E10
void println (int x)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.println(1234);
writer.println(Integer.MAX_VALUE);
}
System.out.print(out);
// Result
// ↓
//1234
//2147483647
void println (long x)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.println(1234L);
writer.println(Long.MAX_VALUE);
}
System.out.print(out);
// Result
// ↓
//1234
//9223372036854775807
void println (Object x)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.println(Path.of("R:", "java-work"));
writer.println(LocalTime.of(14, 30));
}
System.out.print(out);
// Result
// ↓
//R:\java-work
//14:30
void println (String x)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.println("abc");
writer.println("XYZ");
}
System.out.print(out);
// Result
// ↓
//abc
//XYZ
protected void setError ()
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
void write (char[] buf)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.write(new char[]{'a', 'b', 'c'});
writer.write(new char[]{'X', 'Y', 'Z'});
}
System.out.println(out); // abcXYZ
void write (char[] buf, int off, int len)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
final char[] buf = {'a', 'b', 'c', 'd'};
writer.write(buf, 0, 1);
writer.println();
writer.write(buf, 0, 2);
writer.println();
writer.write(buf, 0, 3);
writer.println();
writer.write(buf, 0, 4);
writer.println();
}
System.out.print(out);
// Result
// ↓
//a
//ab
//abc
//abcd
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
final char[] buf = {'a', 'b', 'c', 'd'};
writer.write(buf, 0, 4);
writer.println();
writer.write(buf, 1, 3);
writer.println();
writer.write(buf, 2, 2);
writer.println();
writer.write(buf, 3, 1);
writer.println();
}
System.out.print(out);
// Result
// ↓
//abcd
//bcd
//cd
//d
void write (int c)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.write('a');
writer.write('b');
writer.write('c');
}
System.out.println(out); // abc
void write (String s)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.write("abc");
writer.write("XYZ");
}
System.out.println(out); // abcXYZ
void write (String s, int off, int len)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
final var s = "abcd";
writer.write(s, 0, 1);
writer.println();
writer.write(s, 0, 2);
writer.println();
writer.write(s, 0, 3);
writer.println();
writer.write(s, 0, 4);
writer.println();
}
System.out.print(out);
// Result
// ↓
//a
//ab
//abc
//abcd
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
final var s = "abcd";
writer.write(s, 0, 4);
writer.println();
writer.write(s, 1, 3);
writer.println();
writer.write(s, 2, 2);
writer.println();
writer.write(s, 3, 1);
writer.println();
}
System.out.print(out);
// Result
// ↓
//abcd
//bcd
//cd
//d
Methods declared in Writer
Related posts
- API Examples