我是靠谱客的博主 羞涩跳跳糖,这篇文章主要介绍考研后的Java温习之I/O,现在分享给大家,希望可以做个参考。

感想(feihua)写在前面:

现在距离2016年考研结束已经20天了,在准备考研的过程中2105飞逝而过恍如昨天,直到最后一刻才算是把 所有的知识串了起来,这种考试,最后总是不如意的居多,结果固然重要,最重要的是面对事情的态度和方 法,这一点我想只要经历过高考的人都会点头同意,总之,希望是个好的结局。本来在考前就想考后的第一篇 博客会要怎么样的长篇大论会要如何的意气风发会要如何的如何,20天之后的现在,已是人去楼空客走茶凉的 景象,当时多么渴望键盘的双手现在伏在键盘上却打不出多少字来了。音乐库里已经一年没有更新了,曾经不听歌就睡不着的习惯在学习一天之后回宿舍倒头就睡下变成了回忆。还好计划还在,首先是要把基础回顾一下然后把之前会的不会的都弄会,把想学的尽量学到,当然除了编程之外的很多事也自有安排。一段时光已经
过去。

这几天其实进程很慢,因为专业内部还有考试等等,先是将servlet过了一遍然后学习了jsp这些java ee的部分基础。正准备把代码敲一遍然后学习spring,毕竟seam的应用范围不广,spring既然这么广泛 ,确实是有道理的。在这个过程中将一些容易忘的java基础重新来一遍,这就是本篇博客的出现的原因。
先从I/O开始。

正文开始

废话不多说,直接上代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package test; import java.io.*; import java.util.Date; public class LearnIo { // InputStream-- OutputStream-- Reader-- Writer public void read1() { FileInputStream in = null; try { in = new FileInputStream("D:\note.txt"); } catch (FileNotFoundException e) { System.out.println("Can't find relevent files!"); System.exit(-1); } try { long num = 0; int b = 0; while ((b = in.read()) != -1) { System.out.print((char) b); num++; } in.close(); System.out.println(); System.out.println("一共读取了" + num + "个字符"); } catch (IOException ee) { System.out.println("file can't read correct!"); System.exit(-1); } }// 这样写的话中文出现乱码问题! // +++++++++++++++++++++++++++++++++++++++++++++ public void read2() { FileReader fr = null; try { fr = new FileReader("D:\note.txt"); } catch (FileNotFoundException e) { System.out.println("Can't find relevent files!"); System.exit(-1); } try { long ln = 0; int c = 0; while ((c = fr.read()) != -1) { System.out.print((char) c); ln++; } fr.close(); System.out.println(); System.out.println("一共读取了" + ln + "行"); } catch (IOException ee) { System.out.println("file can't read correct!"); System.exit(-1); } }// // 下面使用缓冲流 BufferedReader/Writer BufferedInputStream/OutputStream public void testbufferStream1() { try { BufferedInputStream bis = new BufferedInputStream( new FileInputStream("D:\note.txt")); int d = 0; System.out.println(bis.read()); System.out.println(bis.read()); bis.mark(30);// 用的比较少! for (int i = 0; i <= 10 && (d = bis.read()) != -1; i++) { System.out.print((char) d + " "); } System.out.println(); bis.reset();// 用的比较少! for (int i = 0; i <= 10 && (d = bis.read()) != -1; i++) { System.out.print((char) d + " "); } bis.close(); } catch (IOException e) { e.printStackTrace(); } } public void testBufferedStream2() { try { BufferedReader br = new BufferedReader(new FileReader( "/home/yourname/javacode/note.txt"));// D:\note.txt // BufferedWriter bw = new BufferedWriter(new FileWriter( // "/home/zsx/javacode/note.txt")); String es = null; while ((es = br.readLine()) != null) { String ess = new String(es.getBytes("utf-8")); System.out.println(ess); // bw.write(e); } // 读取txt文件乱码问题解决! // notice!IO里面涉及了设计模式,BufferedReader经常使用readLine方法,Input/OutputStream // 才有设置字符编码参数的方法,BufferedReader br=new BufferedReader(new // InputStreamReader(new // FileInputStream("path+"/"+title"),"UTF-8")); } catch (IOException ee) { ee.printStackTrace(); } } // 字节数据和字符数据之间的转换InputStreamReader and InputStream OutputStreamWriter and // OutputStream public void testTransform1() { try { OutputStreamWriter osw = new OutputStreamWriter( new FileOutputStream("/home/yourname/javacode/note_1.txt")); osw.write("qwertyuiop"); System.out.println(osw.getEncoding()); osw.close(); osw = new OutputStreamWriter(new FileOutputStream( "/home/yourname/javacode/note_1.txt", true)); osw.write("qwertyuiop学徒学徒学徒"); osw.flush(); osw.close();// 流一定要关闭!关闭前一定要flush! System.out.println("done!"); } catch (IOException ex1) { ex1.printStackTrace(); } } // ctrl+shift+b 设置断点 ,F5是跳进,F6是执行下一步,F7是跳出 public void testTransform2() { BufferedReader br = new BufferedReader(new InputStreamReader(System.in));// System.in是InputStream String s = null; try { s = br.readLine(); while (null != s) { if (s.equalsIgnoreCase("exit")) { System.out .println("program has exited! don't call it again"); break; } System.out.println(s.toUpperCase()); s = br.readLine(); } br.close(); } catch (IOException ex2) { ex2.printStackTrace(); } } // DataInputStream,DataOutputStream 提供处理与机器无关的java 原始类型数据eg.:int float ... public void testDataStream() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); try { dos.writeDouble(Math.random()); dos.writeBoolean(true); DataInputStream dis = new DataInputStream(new ByteArrayInputStream( baos.toByteArray())); System.out.println(dis.readDouble()); System.out.println(dis.readBoolean()); dos.close(); dis.close(); } catch (IOException et) { et.printStackTrace(); } } // Print流 public void testPrintStream1() { PrintStream ps = null; try { FileOutputStream fos = new FileOutputStream( "/home/yourname/javacode/testPrintStream1.txt"); ps = new PrintStream(fos); } catch (IOException e) { e.printStackTrace(); } if (null != ps) { System.out.println(ps); } int line = 0; for (char c = 0; c <= 2000; c++) { System.out.println(c + " "); if (line++ >= 100) { System.out.println(); line = 0; } } } public void testPrintStream2(String f, PrintStream fs) { try { BufferedReader br = new BufferedReader(new FileReader(f)); String s = null; while ((s = br.readLine()) != null) { fs.print(s); } br.close(); } catch (IOException exx) { fs.println("无法读取文件"); } } public void testPrintStream3() { String s = null; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { FileWriter fw = new FileWriter( "/home/yourname/javacode/testPrintStream.log", true); PrintWriter log = new PrintWriter(fw); while ((s = br.readLine()) != null) { if (s.equalsIgnoreCase("exit")) break; System.out.println(s.toUpperCase()); log.println("-----------"); log.println(s.toUpperCase()); log.flush(); } log.println("======" + new Date() + "====="); log.flush(); log.close(); } catch (IOException exxx) { exxx.printStackTrace(); } } public void testObjectIo() { TestClass ts = new TestClass(); ts.tall = 8; try { ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream("/home/yourname/javacode/testObjectIo.dat")); oos.writeObject(ts); oos.flush(); oos.close(); // ~~~~~~~~~~~~~ ObjectInputStream ois = new ObjectInputStream(new FileInputStream( "/home/yourname/javacode/testObjectIo.dat")); TestClass tts; tts = (TestClass) ois.readObject(); System.out.println(tts.tall + " " + tts.name); } catch (IOException e8) { System.out.println("处理出现问题!"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 为了便于测试方法:使用成员内部类 class TestClass { int tall; String name; } public static void main(String[] args) { LearnIo learnIo = new LearnIo(); // learnIo.read1(); // learnIo.read2(); // learnIo.testbufferStream1(); // learnIo.testBufferedStream2(); // learnIo.testTransform1(); // learnIo.testTransform2(); // learnIo.testDataStream(); // learnIo.testPrintStream1(); // learnIo.testPrintStream2("/home/yourname/javacode/note.txt", System.out); // learnIo.testPrintStream3(); learnIo.testObjectIo(); } }

some points of the java IO is located in the annotation. nonsense is unseemliness to this situation.
other points of java will be released soon. I really hope that all things don’t don’t bother me don’t detract my concenteration.

最后

以上就是羞涩跳跳糖最近收集整理的关于考研后的Java温习之I/O的全部内容,更多相关考研后内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部