Java Java类设置 1
此类将对象的格式化表示形式提供给文本输出流。它实现了 PrintStream 中的所有打印方法。它不包含写入原始字节的方法,程序应该使用未编码的字节流。
与 PrintStream 类不同,如果启用了自动刷新,它将仅在调用 println、printf 或格式方法之一时完成,而不是在碰巧输出字符时完成。这些方法使用平台自己的行分隔符概念,而不是字符。
此类中的方法从不抛出 I/O 异常,尽管它的某些构造函数可能会。客户端可以通过调用 checkError() 来查询是否发生了任何错误。
构造函数和描述
- PrintWriter(File file) :使用指定的文件创建一个新的 PrintWriter,不自动刷新行。
- PrintWriter(File file, String csn) :使用指定的文件和字符集创建一个新的 PrintWriter,不自动刷新行。
- PrintWriter(OutputStream out) :从现有的 OutputStream 创建一个新的 PrintWriter,而不自动刷新行。
- PrintWriter(OutputStream out, boolean autoFlush) :从现有的 OutputStream 创建一个新的 PrintWriter。
- PrintWriter(String fileName) :使用指定的文件名创建一个新的 PrintWriter,不自动刷新行。
- PrintWriter(String fileName, String csn) :使用指定的文件名和字符集创建一个新的 PrintWriter,不自动刷新行。
- PrintWriter(Writer out):创建一个新的 PrintWriter,不自动刷新行。
- PrintWriter(Writer out, boolean autoFlush) :创建一个新的 PrintWriter。
方法:
- PrintWriter append(char c) :将指定的字符附加到这个 writer
Syntax :public PrintWriter append(char c) Parameters: c - The 16-bit character to append Returns: This writer
- PrintWriter append(CharSequence csq, int start, int end):将指定的字符序列追加到这个 writer。
Syntax :public PrintWriter append(CharSequence csq, int start, int end) Parameters: csq - The character sequence from which a subsequence will be appended. start - The index of the first character in the subsequence end - The index of the character following the last character in the subsequence Returns:This writer Throws: IndexOutOfBoundsException
- PrintWriter append(CharSequence csq) :将指定字符序列的子序列附加到此编写器。
Syntax :public PrintWriter append(CharSequence csq) Parameters: csq - The character sequence to append. Returns: This writer
- boolean checkError():刷新流并检查其错误状态。
Syntax :public boolean checkError() Returns: true if and only if this stream has encountered an IOException other than InterruptedIOException, or the setError method has been invoked
- protected void clearError() :清除此流的内部错误状态。
Syntax :protected void clearError()
- void close() :关闭流并释放与之关联的任何系统资源。
Syntax :public void close() Specified by:close in class Writer
- void flush():刷新流。
Syntax :public void flush() Specified by:flush in interface Flushable Specified by:flush in class Writer
- PrintWriter format(Locale l, String format, Object... args):使用指定的格式字符串和参数将格式化字符串写入此编写器。
Syntax :public PrintWriter format(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. The number of arguments is variable and may be zero. Returns: This writer Throws: IllegalFormatException NullPointerException
- PrintWriter format(String format, Object... args):使用指定的格式字符串和参数将格式化字符串写入此编写器。
Syntax :public PrintWriter format(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. The number of arguments is variable and may be zero. Returns: This writer Throws: IllegalFormatException NullPointerException
- void print(boolean b):打印一个布尔值。
Syntax :public void print(boolean b)
- void print(char c):打印一个字符。
Syntax :public void print(char c)
- void print(char[] s):打印一个字符数组。
Syntax :public void print(char[] s)
- void print(double d) :打印一个双精度浮点数。
Syntax :public void print(double b)
- void print(float f):打印一个浮点数。
Syntax :public void print(float f)
- void print(int i):打印一个整数。
Syntax :public void print(int i)
- void print(long l):打印一个长整数。
Syntax :public void print(long l)
- void print(Object obj) :打印一个对象。
Syntax :public void print(Object obj)
- void print(String s):打印一个字符串。
Syntax :public void print(String s)
程序:
import java.io.*;
import java.util.Locale;
//Java program to demonstrate PrintWriter
class PrintWriterDemo {
public static void main(String[] args)
{
String s="GeeksforGeeks";
// create a new writer
PrintWriter out = new PrintWriter(System.out);
char c[]={'G','E','E','K'};
//illustrating print(boolean b) method
out.print(true);
//illustrating print(int i) method
out.print(1);
//illustrating print(float f) method
out.print(4.533f);
//illustrating print(String s) method
out.print("GeeksforGeeks");
out.println();
//illustrating print(Object Obj) method
out.print(out);
out.println();
//illustrating append(CharSequence csq) method
out.append("Geek");
out.println();
//illustrating checkError() method
out.println(out.checkError());
//illustrating format() method
out.format(Locale.UK, "This is my %s program", s);
//illustrating flush method
out.flush();
//illustrating close method
out.close();
}
}
Output:
true14.533GeeksforGeeks
java.io.PrintWriter@1540e19d
Geek
false
This is my GeeksforGeeks program
下一篇: Java.io.PrintWriter 类Java |设置 2