Java PrintStream
类
原文:https://www.studytonight.com/java-file-io/java-printstream-class
在本教程中,我们将学习 Java 中的PrintStream
类。这个类提供了各种将数据写入另一个流的方法。此类将原始数据转换为文本格式,并将其写入输出流。
与其他输出流不同,a
PrintStream
从不抛出IOException
;或者,特殊情况设置一个内部标志,可以通过checkError
方法进行测试。
句法
这是PrintStream
类的语法声明。
public class PrintStream extends FilterOutputStream implements Closeable. Appendable
PrintStream
类的方法
方法 | 描述 |
---|---|
void print(boolean b) |
它打印指定的布尔值。 |
void print(char c) |
它打印指定的字符值。 |
void print(char[] c) |
它打印指定的字符数组值。 |
void print(int i) |
它打印指定的 int 值。 |
void print(long l) |
它打印指定的长值。 |
void print(float f) |
它打印指定的浮点值。 |
void print(double d) |
它打印指定的双精度值。 |
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) |
它打印指定的长值并终止该行。 |
void println(float f) |
它打印指定的浮点值并终止该行。 |
void println(double d) |
它打印指定的双精度值并终止该行。 |
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) |
它使用指定的格式将格式化的字符串写入当前流。 |
PrintStream println()方法示例
在下面给出的程序中,我们正在从PrintStream
类中实现println()
方法,该方法将打印数据。我们有两种变化不大的方法:
print()
-该方法将给定的数据打印到输出流中。
println()
-该方法将数据打印到输出流中,并将新行添加到输出的末尾。
在下面的程序中,我们没有创建PrintStream
类的实例,但是我们可以直接调用PrintStream
类的 print()
方法。原因是System
是最后一个班,在那里PrintStream
型被宣布出局。
package studytonight;
public class StudyTonight
{
public static void main(String args[])
{
System.out.println("Hello Studytonight");
}
}
你好,今晚学习
PrintStream
打印()方法示例
在下面的例子中,我们创建了一个PrintStream
类的对象,这个流将存储在提供的文件中。使用 PrintStream 类的对象调用 print()方法后,它会将数据存储到给定的文件中。我们可以看到 output.txt 的内容,并验证它是否按预期工作。
package studytonight;
import java.io.PrintStream;
public class StudyTonight
{
public static void main(String args[])
{
String str = "Welcome to studytonight.com";
try
{
PrintStream obj= new PrintStream("E:\\studytonight\\output.txt");
obj.print(str);
obj.close();
System.out.print("Data is written to the file successfully...");
}
catch(Exception e)
{
System.out.println("Error: "+e.toString());
}
}
}
数据成功写入文件...
输出. txt
Welcome to studytonight.com
PrintStream
示例
在给定的例子中,我们实现了一个带有两个参数的print()
方法,第一个参数是格式化的字符串,第二个参数是一个值。在本例中%d
将被12
取代,数据将作为I have 12 apples.
写入文件。这种技术被称为格式化输出。
package studytonight;
import java.io.PrintStream;
public class StudyTonight
{
public static void main(String args[])
{
try
{
PrintStream output = new PrintStream("E:\\studytonight\\output.txt");
int num = 12;
output.printf("I have %d apples.", num);
output.close();
System.out.print("Data is written to the file successfully...");
}
catch(Exception e)
{
System.out.println("Error: "+e.toString());
}
}
}
数据成功写入文件...
输出. txt
I have 12 apples.
结论
在本教程中,我们学习了 Java 中的 PrintStream 类。这个类提供了各种将数据写入另一个流的方法。此类将原始数据转换为文本格式,并将其写入输出流。