java IO之PrintStream和PrintWriter

來源:互聯網
上載者:User
 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 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.