@@ -12,15 +12,17 @@ tags:
12
12
13
13
# Java IO
14
14
15
- <!-- TOC depthFrom:2 depthTo:3 -->
15
+ <!-- TOC depthFrom:2 depthTo:2 -->
16
16
17
- - [ File 和 RandomAccessFile] ( #file-和-randomaccessfile )
18
- - [ File 类] ( #file-类 )
19
- - [ RandomAccessFile 类] ( #randomaccessfile-类 )
17
+ - [ File] ( #file )
18
+ - [ RandomAccessFile] ( #randomaccessfile )
19
+ - [ System] ( #system )
20
+ - [ Scanner] ( #scanner )
20
21
- [ 字符流和字节流] ( #字符流和字节流 )
21
- - [ 字符流和字节流的区别] ( #字符流和字节流的区别 )
22
22
- [ FileReader 和 FileWriter] ( #filereader-和-filewriter )
23
23
- [ InputStreamReader 和 OutputStreamWriter] ( #inputstreamreader-和-outputstreamwriter )
24
+ - [ BufferedReader] ( #bufferedreader )
25
+ - [ PrintStream] ( #printstream )
24
26
- [ FileInputStream 和 FileOutputStream] ( #fileinputstream-和-fileoutputstream )
25
27
- [ ByteArrayInputStream 和 ByteArrayOutputStream] ( #bytearrayinputstream-和-bytearrayoutputstream )
26
28
- [ PipedInputStream 和 PipedOutputStream] ( #pipedinputstream-和-pipedoutputstream )
@@ -30,15 +32,13 @@ tags:
30
32
31
33
<!-- /TOC -->
32
34
33
- ## File 和 RandomAccessFile
34
-
35
- ### File 类
35
+ ## File
36
36
37
37
` File ` 类是 ` java.io ` 包中唯一对文件本身进行操作的类。它可以对文件、目录进行增删查操作。
38
38
39
- #### 常用方法
39
+ ### 常用方法
40
40
41
- ##### createNewFille
41
+ #### createNewFille
42
42
43
43
可以使用 ` createNewFille() ` 方法创建一个新文件。
44
44
@@ -57,7 +57,7 @@ File f = new File(filename);
57
57
boolean flag = f. createNewFile();
58
58
```
59
59
60
- ##### mkdir
60
+ #### mkdir
61
61
62
62
可以使用 ` mkdir() ` 来创建文件夹,但是如果要创建的目录的父路径不存在,则无法创建成功。
63
63
@@ -70,7 +70,7 @@ File f = new File(filename);
70
70
boolean flag = f. mkdir();
71
71
```
72
72
73
- ##### delete
73
+ #### delete
74
74
75
75
可以使用 ` delete() ` 来删除文件或目录。
76
76
@@ -83,7 +83,7 @@ File f = new File(filename);
83
83
boolean flag = f. delete();
84
84
```
85
85
86
- ##### list 和 listFiles
86
+ #### list 和 listFiles
87
87
88
88
` File ` 中给出了两种列出文件夹内容的方法:
89
89
@@ -104,7 +104,7 @@ File f = new File(filename);
104
104
File files[] = f. listFiles();
105
105
```
106
106
107
- ### RandomAccessFile 类
107
+ ## RandomAccessFile
108
108
109
109
> 注:` RandomAccessFile ` 类虽然可以实现对文件内容的读写操作,但是比较复杂。所以一般操作文件内容往往会使用字节流或字符流方式。
110
110
@@ -114,7 +114,7 @@ File files[] = f.listFiles();
114
114
115
115
文件中记录的大小不一定都相同,只要能够确定哪些记录有多大以及它们在文件中的位置即可。
116
116
117
- #### 写操作
117
+ ### 写操作
118
118
119
119
当用 ` rw ` 方式声明 ` RandomAccessFile ` 对象时,如果要写入的文件不存在,系统将自行创建。
120
120
@@ -148,7 +148,7 @@ public class RandomAccessFileDemo01 {
148
148
}
149
149
```
150
150
151
- #### 读操作
151
+ ### 读操作
152
152
153
153
读取是直接使用 ` r ` 的模式即可,以只读的方式打开文件。
154
154
@@ -192,6 +192,116 @@ public class RandomAccessFileDemo02 {
192
192
}
193
193
```
194
194
195
+ ## System
196
+
197
+ System 中提供了三个常用于 IO 的静态成员:
198
+
199
+ - System.out
200
+ - System.err
201
+ - System.in
202
+
203
+ 示例:重定向 System.out 输出流
204
+
205
+ ``` java
206
+ import java.io.* ;
207
+ public class SystemOutDemo {
208
+
209
+ public static void main (String args []) throws Exception {
210
+ OutputStream out = new FileOutputStream (" d:\\ test.txt" );
211
+ PrintStream ps = new PrintStream (out);
212
+ System . setOut(ps);
213
+ System . out. println(" 人生若只如初见,何事秋风悲画扇" );
214
+ ps. close();
215
+ out. close();
216
+ }
217
+ }
218
+ ```
219
+
220
+ 示例:重定向 System.err 输出流
221
+
222
+ ``` java
223
+ public class SystemErrDemo {
224
+
225
+ public static void main (String args []) throws IOException {
226
+ OutputStream bos = new ByteArrayOutputStream (); // 实例化
227
+ PrintStream ps = new PrintStream (bos); // 实例化
228
+ System . setErr(ps); // 输出重定向
229
+ System . err. print(" 此处有误" );
230
+ System . out. println(bos); // 输出内存中的数据
231
+ }
232
+ }
233
+ ```
234
+
235
+ 示例:接受控制台输入信息
236
+
237
+ ``` java
238
+ import java.io.* ;
239
+ public class SystemInDemo {
240
+
241
+ public static void main (String args []) throws IOException {
242
+ InputStream input = System . in;
243
+ StringBuffer buf = new StringBuffer ();
244
+ System . out. print(" 请输入内容:" );
245
+ int temp = 0 ;
246
+ while ((temp = input. read()) != - 1 ) {
247
+ char c = (char ) temp;
248
+ if (c == ' \n ' ) {
249
+ break ;
250
+ }
251
+ buf. append(c);
252
+ }
253
+ System . out. println(" 输入的内容为:" + buf);
254
+ input. close();
255
+ }
256
+ }
257
+ ```
258
+
259
+ ## Scanner
260
+
261
+ Scanner 可以完成输入数据操作,并对数据进行验证。
262
+
263
+ 示例:
264
+
265
+ ``` java
266
+ import java.io.* ;
267
+ public class ScannerDemo {
268
+
269
+ public static void main (String args []) {
270
+ Scanner scan = new Scanner (System . in); // 从键盘接收数据
271
+ int i = 0 ;
272
+ float f = 0.0f ;
273
+ System . out. print(" 输入整数:" );
274
+ if (scan. hasNextInt()) { // 判断输入的是否是整数
275
+ i = scan. nextInt(); // 接收整数
276
+ System . out. println(" 整数数据:" + i);
277
+ } else {
278
+ System . out. println(" 输入的不是整数!" );
279
+ }
280
+
281
+ System . out. print(" 输入小数:" );
282
+ if (scan. hasNextFloat()) { // 判断输入的是否是小数
283
+ f = scan. nextFloat(); // 接收小数
284
+ System . out. println(" 小数数据:" + f);
285
+ } else {
286
+ System . out. println(" 输入的不是小数!" );
287
+ }
288
+
289
+ Date date = null ;
290
+ String str = null ;
291
+ System . out. print(" 输入日期(yyyy-MM-dd):" );
292
+ if (scan. hasNext(" ^\\ d{4}-\\ d{2}-\\ d{2}$" )) { // 判断
293
+ str = scan. next(" ^\\ d{4}-\\ d{2}-\\ d{2}$" ); // 接收
294
+ try {
295
+ date = new SimpleDateFormat (" yyyy-MM-dd" ). parse(str);
296
+ } catch (Exception e) {}
297
+ } else {
298
+ System . out. println(" 输入的日期格式错误!" );
299
+ }
300
+ System . out. println(date);
301
+ }
302
+ }
303
+ ```
304
+
195
305
## 字符流和字节流
196
306
197
307
JAVA IO 中的流操作分为两类:
@@ -307,6 +417,60 @@ public class InputStreamReaderDemo {
307
417
}
308
418
```
309
419
420
+ ## BufferedReader
421
+
422
+ BufferedReader 类用于从缓冲区中读取内容,所有的输入字节数据都放在缓冲区中。
423
+
424
+ 示例:
425
+
426
+ ``` java
427
+ import java.io.BufferedReader ;
428
+ import java.io.IOException ;
429
+ import java.io.InputStreamReader ;
430
+
431
+ public class BufferedReaderDemo {
432
+
433
+ public static void main (String args []) throws IOException {
434
+ BufferedReader buf = new BufferedReader (new InputStreamReader (System . in));
435
+ while (true ) {
436
+ System . out. print(" 请输入内容:" );
437
+ String str = buf. readLine();
438
+ if (str. equalsIgnoreCase(" exit" )) {
439
+ System . out. print(" 退出" );
440
+ break ;
441
+ }
442
+ System . out. println(" 输入的内容为:" + str);
443
+ }
444
+ }
445
+ }
446
+ ```
447
+
448
+ ## PrintStream
449
+
450
+ PrintStream 提供了非常方便的打印功能。
451
+
452
+ 事实上,我们常用的 System 中提供的静态成员 System.out 和 System.err 就是 PrintStream 对象。
453
+
454
+ 示例:
455
+
456
+ ``` java
457
+ import java.io.* ;
458
+
459
+ public class PrintStreamDemo {
460
+
461
+ public static void main (String arg []) throws Exception {
462
+ final String filepath = " d:\\ test.txt" ;
463
+ // 如果现在是使用 FileOuputStream 实例化,意味着所有的数据都会输出到文件中
464
+ OutputStream os = new FileOutputStream (new File (filepath));
465
+ PrintStream ps = new PrintStream (os);
466
+ ps. print(" Hello " );
467
+ ps. println(" World!!!" );
468
+ ps. printf(" 姓名:%s;年龄:%d" , " 张三" , 18 );
469
+ ps. close();
470
+ }
471
+ }
472
+ ```
473
+
310
474
## FileInputStream 和 FileOutputStream
311
475
312
476
** FileInputStream 和 FileOutputStream 用于输入、输出文件。**
0 commit comments