java IO之PrintStream和PrintWriter

 1  package com.io.test;
 2 
 3  import java.io.BufferedReader;
 4  import java.io.FileNotFoundException;
 5  import java.io.FileOutputStream;
 6  import java.io.FileWriter;
 7  import java.io.IOException;
 8  import java.io.InputStreamReader;
 9  import java.io.PrintStream;
10  import java.io.PrintWriter;
11  import java.util.Date;
12 
13  import org.junit.Test;
14 
15  /**
16   * 打印字节
17   *  @author  chengjj
18   *
19    */
20  public  class TestPrintStreamAndPrinter {
21     @Test
22      public  void test() {
23          try {
24             FileOutputStream os =  new FileOutputStream("E:/1.txt");
25             PrintStream ps =  new PrintStream(os);
26              if (ps !=  null) {
27                 System.setOut(ps);
28             }
29              int ln = 0;
30              for ( char i = 0; i < 65536; i++) {
31                 System.out.print(i + "");
32                  if ((ln ++) % 100 == 0) {
33                     System.out.println();
34                 }
35             }
36         }  catch (FileNotFoundException e) {
37             e.printStackTrace();
38         }
39     }
40     
41     @Test
42      public  void test1(){
43          try {
44             BufferedReader reader =  new BufferedReader( new InputStreamReader(System.in));
45             FileWriter writer =  new FileWriter("E:/logs.log", true);
46             PrintWriter log =  new PrintWriter(writer);
47             String s = reader.readLine();
48              while(s !=  null){
49                  if(s.equalsIgnoreCase("exit"))
50                      break;
51                 System.out.println(s);
52                 log.println(s);
53                 log.println( new Date());
54                 log.println("-------------------------------------------");
55                 log.flush(); // 自动带flush功能,不过写上也没有什么
56                  s = reader.readLine();
57             }
58             reader.close();
59             log.close();
60             
61         }  catch (IOException e) {
62             e.printStackTrace();
63         }
64         
65         
66     }
67     
68     
69 }

你可能感兴趣的:(java io)