Java中的快速输入输出

 1 /*
 2 使用BufferedReader和BufferedWriter实现快速输入输出
 3 
 4 BufferedReader 和 BufferedWriter 都在 java.io.*包内。
 5 
 6 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
 7 主要使用read() 和 readLine()方法
 8 
 9 int read()                                            读取单个字符。
10 String readLine()                                读取一个文本行。
11 
12 需要注意的是 在windows中按一下回车键 一共有两个字符 "\n\r"  而read()只能读取一个字符所以如要要用read来达到吸收回车的目的,需要用两个read();  如果用readLine()的话会将"\n\r"全部吸收 , 所以只需要一个readLine()来吸收回车。
13 
14 
15 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
16 
17 主要使用 BufferedWriter类中的 write() 类进行输出
18 
19 需要注意的是 write() 不能直接输出int类型, 因为write(int a)  会输出其对应的ASCii码的字符。
20 
21 所以当需要输出一个int类型的变量时, 可以用Integer.toString(int a)方法 将其变为字符串形式输出。
22 或者使用 + 拼接一个字符串,这样 参数整体就是一个字符串了,比如加一个换行符。
23 
24 
25 
26 */
27 
28 //实例
29 
30 import java.io.*;
31 
32 import java.util.*;
33 
34  
35 
36  
37 
38 public class Main{
39 
40     static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
41 
42     static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
43 
44     public static void main(String[] args) throws IOException{
45 
46         //测试writr 不能直接输出int类型
47 
48         int a = 65;
49 
50         out.write(a);
51 
52         out.write("\n");
53 
54         out.write(a + "\n");  // 使用 + 号拼接个字符串 使参数整体为一个字符串 
55 
56         out.write(Integer.toString(a)); // 输出a的字符串形式
57 
58         out.write("\n");
59 
60        
61 
62         //测试 read() 和  readLine();
63 
64         int b = in.read();   // read()只读取一个字符
65 
66         int c = in.read();   // 吸收 \n
67 
68         int x = in.read();   // 吸收 \r
69 
70        // String e = in.readLine();
71 
72         String d = in.readLine();
73 
74         out.write("\n");
75 
76         out.write(b + "\n");
77 
78         out.write(c + "\n");
79 
80         out.write(x + "\n");
81 
82         out.write(d + "\n");
83 
84         //out.write(e);
85 
86         out.flush();
87 
88     }
89 
90 }

 

 

posted @ 2020-08-09 14:22  vkd  阅读(712)  评论(0编辑  收藏  举报