Java PrintStream 类2024 年 8 月 29 日 | 阅读 2 分钟 PrintStream 类提供了向另一个流写入数据的方法。PrintStream 类会自动刷新数据,因此无需调用 flush() 方法。此外,其方法不抛出 IOException。
类声明让我们看看 Java.io.PrintStream 类的声明
PrintStream 类的方法方法 | 描述 |
---|
void print(boolean b) | 它打印指定的布尔值。 | void print(char c) | 它打印指定的字符值。 | void print(char[] c) | 它打印指定的字符数组值。 | void print(int i) | 它打印指定的 int 值。 | void print(long l) | 它打印指定的 long 值。 | void print(float f) | 它打印指定的 float 值。 | void print(double d) | 它打印指定的 double 值。 | void print(String s) | 它打印指定的字符串值。 | void print(Object obj) | 它打印指定的对象值。 | void println(boolean b) | 它打印指定的布尔值并终止行。 | void println(char c) | 它打印指定的字符值并终止行。 | void println(char[] c) | 它打印指定的字符数组值并终止行。 | void println(int i) | 它打印指定的 int 值并终止行。 | void println(long l) | 它打印指定的 long 值并终止行。 | void println(float f) | 它打印指定的 float 值并终止行。 | void println(double d) | 它打印指定的 double 值并终止行。 | void println(String s) | 它打印指定的字符串值并终止行。 | void println(Object obj) | 它打印指定的对象值并终止行。 | void println() | 它只终止行。 | void printf(Object format, Object... args) | 它将格式化字符串写入当前流。 | void printf(Locale l, Object format, Object... args) | 它将格式化字符串写入当前流。 | void format(Object format, Object... args) | 它使用指定的格式将格式化字符串写入当前流。 | void format(Locale l, Object format, Object... args) | 它使用指定的格式将格式化字符串写入当前流。 |
Java PrintStream 类的示例在这个例子中,我们只是简单地打印整数和字符串值。 输出 文本文件 testout.txt 的内容设置为以下数据 2016
Hello Java
Welcome to Java
使用 java PrintStream 类的 printf() 方法示例让我们看看使用 java.io.PrintStream 类的 printf() 方法通过格式说明符打印整数值的简单示例。 输出
|