打印流PrintStream

时间:2020-05-13
本文章向大家介绍打印流PrintStream,主要包括打印流PrintStream使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

打印流PrintStream

  • public class PrintStream
    extends FilterOutputStream
    implements Appendable, Closeable

    A PrintStream为另一个输出流添加了功能,即能够方便地打印各种数据值的表示。还提供了另外两个功能。与其他输出流不同, PrintStream从不抛出IOException ;相反,异常情况只是设置一个可以通过checkError方法测试的内部标志。可以选择一个PrintStream ,以便自动刷新;这意味着flush字节数组写入方法后自动调用,所述一个println方法被调用时,或者一个新行字符或字节( '\n' )被写入。

    由印刷的所有字符PrintStream被转换成使用平台的默认字符编码字节。 在需要编写字符而不是字节的情况下,应使用PrintWriter类。

    • Constructor and Description
      PrintStream(File file) 使用指定的文件创建一个新的打印流,而不需要自动换行。
      PrintStream(File file, String csn) 使用指定的文件和字符集创建新的打印流,而不需要自动换行。
      PrintStream(OutputStream out) 创建一个新的打印流。
      PrintStream(OutputStream out, boolean autoFlush) 创建一个新的打印流。
      PrintStream(OutputStream out, boolean autoFlush, String encoding) 创建一个新的打印流。
      PrintStream(String fileName) 使用指定的文件名创建新的打印流,无需自动换行。
      PrintStream(String fileName, String csn) 创建一个新的打印流,不需要自动换行,具有指定的文件名和字符集

public class Demo07Main {
  public static void main(String[] args) {
      PrintStream printStream = null;
      try {
          printStream = new PrintStream("io\\f.txt");
          printStream.println("你好");
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      }finally {
          if (printStream != null){
              printStream.close();
          }
      }

  }
}

try {
  PrintStream printStream = new PrintStream("io\\打印输出地址.txt");
  System.setOut(printStream);//把输出语句的目的地改为了打印流的目的地
  System.out.println("hahhhah");
  printStream.close();
} catch (FileNotFoundException e) {
  e.printStackTrace();
}

 

原文地址:https://www.cnblogs.com/lxy522/p/12881821.html