📌  相关文章
📜  Java I/O-PrintStream类

📅  最后修改于: 2020-09-27 07:01:19             🧑  作者: Mango

Java PrintStream类

PrintStream类提供了将数据写入另一个流的方法。 PrintStream类自动刷新数据,因此无需调用flush()方法。而且,它的方法不会抛出IOException。

类声明

让我们看一下Java.io.PrintStream类的声明:

public class PrintStream extends FilterOutputStream implements Closeable. Appendable 

PrintStream类的方法

Method Description
void print(boolean b) It prints the specified boolean value.
void print(char c) It prints the specified char value.
void print(char[] c) It prints the specified character array values.
void print(int i) It prints the specified int value.
void print(long l) It prints the specified long value.
void print(float f) It prints the specified float value.
void print(double d) It prints the specified double value.
void print(String s) It prints the specified string value.
void print(Object obj) It prints the specified object value.
void println(boolean b) It prints the specified boolean value and terminates the line.
void println(char c) It prints the specified char value and terminates the line.
void println(char[] c) It prints the specified character array values and terminates the line.
void println(int i) It prints the specified int value and terminates the line.
void println(long l) It prints the specified long value and terminates the line.
void println(float f) It prints the specified float value and terminates the line.
void println(double d) It prints the specified double value and terminates the line.
void println(String s) It prints the specified string value and terminates the line.
void println(Object obj) It prints the specified object value and terminates the line.
void println() It terminates the line only.
void printf(Object format, Object… args) It writes the formatted string to the current stream.
void printf(Locale l, Object format, Object… args) It writes the formatted string to the current stream.
void format(Object format, Object… args) It writes the formatted string to the current stream using specified format.
void format(Locale l, Object format, Object… args) It writes the formatted string to the current stream using specified format.

Java PrintStream类的示例

在此示例中,我们仅打印整数和字符串值。

package com.javatpoint;

import java.io.FileOutputStream;
import java.io.PrintStream;
public class PrintStreamTest{  
 public static void main(String args[])throws Exception{  
   FileOutputStream fout=new FileOutputStream("D:\\testout.txt ");  
   PrintStream pout=new PrintStream(fout);  
   pout.println(2016);  
   pout.println("Hello Java");  
   pout.println("Welcome to Java");  
   pout.close();  
   fout.close();  
  System.out.println("Success?");  
 }  
}  
 

输出量

Success...

文本文件testout.txt的内容使用以下数据设置

2016
Hello Java
Welcome to Java

使用Java PrintStream类的printf()方法的示例:

让我们看一下使用java.io.PrintStream类的printf()方法通过格式说明符打印整数值的简单示例。

class PrintStreamTest{  
 public static void main(String args[]){  
   int a=19;  
   System.out.printf("%d",a); //Note: out is the object of printstream     
 }  
} 

输出量

19