Java I/O系统之Print 流

来源:互联网 发布:中国网络直播行业协会 编辑:程序博客网 时间:2024/05/16 11:32

1. Print 流介绍

1)        PrintWriter和PrintStream都属于输出流,分别针对与字符和字节。

2)        PrintWriter和PrintStream提供了重载的print。

3)        Println方法用于多种数据类型的输出。

4)        PrintWriter和PrintStream的输出操作不会抛出异常,用户通过检测错误状态获取错误信息。

5)        PrintWriter和PrintStream有自动flush功能。

2. Print 流常用函数

1)        PrintWriter(Writer out)

2)        PrintWriter(Writer out,booleanautoFlush)

3)        PrintWriter(OutputStream out)

4)        PrintWriter(OutputStreamout,boolean autoFlush)

5)        PrintStream(OutputStream out)

6)        PrintStream(OutputStreamout,boolean autoFlush)

3. Print 流例子一

[java] view plain copy
print?
  1. package com.owen.io;  
  2.   
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.PrintStream;  
  6.   
  7. /** 
  8.  * Print流的使用 
  9.  *  
  10.  * @author OwenWilliam 2016-7-20 
  11.  * @since 
  12.  * @version v1.0.0 
  13.  * 
  14.  */  
  15. public class TestPrintStream1  
  16. {  
  17.   
  18.     public static void main(String[] args)  
  19.     {  
  20.         PrintStream ps = null;  
  21.         try  
  22.         {  
  23.             // 将内容读出到指定文件中  
  24.             FileOutputStream fos = new FileOutputStream("E:\\test\\log.dat");  
  25.             // 使用PrintStream不会抛出异常,关于所有的print的操作都与其有关  
  26.             ps = new PrintStream(fos);  
  27.         } catch (IOException e)  
  28.         {  
  29.             e.printStackTrace();  
  30.         }  
  31.   
  32.         // setOut是将输出的流“管道”对准指定的文件中输出,而不是我们的命令窗口  
  33.         if (ps != null)  
  34.         {  
  35.             System.setOut(ps);  
  36.         }  
  37.   
  38.         int ln = 0;  
  39.         // 输出到控制台,同时也会写入上面的文件  
  40.         for (char c = 0; c <= 60000; c++)  
  41.         {  
  42.             System.out.print(c + " ");  
  43.             if (ln++ >= 100)  
  44.             {  
  45.                 System.out.println();  
  46.                 ln = 0;  
  47.             }  
  48.         }  
  49.     }  
  50.   
  51. }  

4. Print 流例子二

[java] view plain copy
print?
  1. package com.owen.io;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.FileReader;  
  5. import java.io.IOException;  
  6. import java.io.PrintStream;  
  7.   
  8. /** 
  9.  * Print流文件读出 
  10.  * @author OwenWilliam 2016-7-20 
  11.  * @since 
  12.  * @version v1.0.0 
  13.  * 
  14.  */  
  15. public class TestPrintStream2  
  16. {  
  17.   
  18.     public static void main(String[] args)  
  19.     {  
  20.         String filename = "E:/workspace/Java/IO/src/com/owen/io/TestPrintStream2.java";  
  21.         if (filename != null)  
  22.         {  
  23.             list (filename, System.out);  
  24.         }  
  25.     }  
  26.   
  27.     /** 
  28.      * 实现指定文件读出 
  29.      * @param f 文件路径 
  30.      * @param fs 读出 
  31.      */  
  32.     private static void list(String f, PrintStream fs)  
  33.     {  
  34.         try  
  35.         {  
  36.             BufferedReader br = new BufferedReader(new FileReader(f));  
  37.             String s = null;  
  38.             while ((s = br.readLine()) != null)  
  39.             {  
  40.                 fs.println(s);  
  41.             }  
  42.             br.close();  
  43.         } catch (IOException e)  
  44.         {  
  45.             fs.println("无法读取文件");  
  46.         }  
  47.     }  
  48.   
  49. }  

5. Print 流例子三

[java] view plain copy
print?
  1. package com.owen.io;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.FileWriter;  
  5. import java.io.IOException;  
  6. import java.io.InputStreamReader;  
  7. import java.io.PrintWriter;  
  8. import java.util.Date;  
  9.   
  10. /** 
  11.  * 使用Print流实现日志功能 
  12.  * @author OwenWilliam 2016-7-20 
  13.  * @since 
  14.  * @version v1.0.0 
  15.  * 
  16.  */  
  17. public class TestPrintStream3  
  18. {  
  19.   
  20.     public static void main(String[] args)  
  21.     {  
  22.         String s = null;  
  23.         BufferedReader br = new BufferedReader(  
  24.                           new InputStreamReader(System.in));  
  25.         try  
  26.         {  
  27.             //文件写入地方  
  28.             FileWriter fw = new FileWriter("E:\\test\\logfile.log",true);  
  29.             PrintWriter log = new PrintWriter(fw);  
  30.             //一行一行写入  
  31.             while ((s = br.readLine()) != null)  
  32.             {  
  33.                 if (s.equalsIgnoreCase("exit")) break;  
  34.                 System.out.println(s.toUpperCase());  
  35.                 log.println("-------");  
  36.                 log.println(s.toUpperCase());  
  37.                 log.flush();  
  38.             }  
  39.             //结束记录时间  
  40.             log.println("====" + new Date() + "====");  
  41.             log.flush();  
  42.             log.close();  
  43.         } catch (IOException e)  
  44.         {  
  45.             e.printStackTrace();  
  46.         }  
  47.     }  
  48.   
  49. }