PrintStream and PrintWriter of java IO

Source: Internet
Author: 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 * print bytes
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 (); // automatically includes the flush function, but there is nothing to write.
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}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.