zoukankan      html  css  js  c++  java
  • 打印流

    打印流

    在整个 IO 包中,打印流是输出信息最方便的类,主要包含字节打印流(PrintStream) 和字符打印流(PrintWriter) . 打印流提供了非常方便的打印功能,可以打印任何的数据类型,例如: 小数、整数、字符串等等。

    回顾:之前在打印信息的时候需要使用OutputStream, 但是这样一来,所有的数据输出的时候会非常的麻烦, String --> byte[], 打印流中可以方便的进行输出。


    在这个类中定义了很多print() 或println() 方法。System.out.println(), 此方法可以打印任何数据类型。

    构造方法:

    public PrintStream(OutputStream out) -->指定输出位置。

    此构造方法接收OutputStream 的子类。

    使用 PrintStream 输出信息

    [java] view plain copy
    1. import java.io.* ;  
    2. public class PrintDemo01{  
    3.     public static void main(String arg[]) throws Exception{  
    4.         PrintStream ps = null ;     // 声明打印流对象  
    5.         // 如果现在是使用FileOuputStream实例化,意味着所有的输出是向文件之中  
    6.         ps = new PrintStream(new FileOutputStream(new File("d:" + File.separator + "test.txt"))) ;  
    7.         ps.print("hello ") ;  
    8.         ps.println("world!!!") ;  
    9.         ps.print("1 + 1 = " + 2) ;  
    10.         ps.close() ;  
    11.     }  
    12. };  

    也就是说此时,实际上是将FileOutputStream 类的功能包装了一下。这样的设计在JAVA中称为装饰设计。

    2、格式化输出

    如果学习过其他语言,比较代表性的就是C语言。

    [java] view plain copy
    1. import java.io.* ;  
    2. public class PrintDemo02{  
    3.     public static void main(String arg[]) throws Exception{  
    4.         PrintStream ps = null ;     // 声明打印流对象  
    5.         // 如果现在是使用FileOuputStream实例化,意味着所有的输出是向文件之中  
    6.         ps = new PrintStream(new FileOutputStream(new File("d:" + File.separator + "test.txt"))) ;  
    7.         String name = "李兴华" ;   // 定义字符串  
    8.         int age = 30 ;              // 定义整数  
    9.         float score = 990.356f ;    // 定义小数  
    10.         char sex = 'M' ;            // 定义字符  
    11.         ps.printf("姓名:%s;年龄:%d;成绩:%f;性别:%c",name,age,score,sex) ;  
    12.         ps.close() ;  
    13.     }  
    14. };  

    如果,觉得以上的要写很多%s、%d 无法记住的话呢,实际上也可以简单操作,全部使用%s表示。


    [java] view plain copy
    1. import java.io.* ;  
    2. public class PrintDemo03{  
    3.     public static void main(String arg[]) throws Exception{  
    4.         PrintStream ps = null ;     // 声明打印流对象  
    5.         // 如果现在是使用FileOuputStream实例化,意味着所有的输出是向文件之中  
    6.         ps = new PrintStream(new FileOutputStream(new File("d:" + File.separator + "test.txt"))) ;  
    7.         String name = "李兴华" ;   // 定义字符串  
    8.         int age = 30 ;              // 定义整数  
    9.         float score = 990.356f ;    // 定义小数  
    10.         char sex = 'M' ;            // 定义字符  
    11.         ps.printf("姓名:%s;年龄:%s;成绩:%s;性别:%s",name,age,score,sex) ;  
    12.         ps.close() ;  
    13.     }  
    14. };  
    总结:

    1、PrintStream 可以方便的完成输出的功能。

    2、在以后的输出中基本上都使用PrintStream 完成,因为比较方便一些。

    3、PrintStream 属于装饰设计模式。





  • 相关阅读:
    【JavaScript】JavaScript学习 四 JavaScript 语句
    【Javascript】javascript学习 十二 JavaScript 函数
    【Javascript】javascript学习 六 七 JavaScript 变量/运算符
    【JavaScript】JavaScript学习 三 把 JavaScript 放置到何处
    【Javascript】javascript学习 十四 JavaScript Break 和 Continue
    【Javascript】javascript学习 十 JavaScript 消息框
    如何查询数据库的主键约束schema?
    无限级下拉列表的无限级分级[asp.net](转)
    结构体
    对typedef的了解
  • 原文地址:https://www.cnblogs.com/Jxiaobai/p/6622201.html
Copyright © 2011-2022 走看看