@@ -12,15 +12,17 @@ tags:
1212
1313# Java IO
1414
15- <!-- TOC depthFrom:2 depthTo:3 -->
15+ <!-- TOC depthFrom:2 depthTo:2 -->
1616
17- - [ File 和 RandomAccessFile] ( #file-和-randomaccessfile )
18- - [ File 类] ( #file-类 )
19- - [ RandomAccessFile 类] ( #randomaccessfile-类 )
17+ - [ File] ( #file )
18+ - [ RandomAccessFile] ( #randomaccessfile )
19+ - [ System] ( #system )
20+ - [ Scanner] ( #scanner )
2021- [ 字符流和字节流] ( #字符流和字节流 )
21- - [ 字符流和字节流的区别] ( #字符流和字节流的区别 )
2222- [ FileReader 和 FileWriter] ( #filereader-和-filewriter )
2323- [ InputStreamReader 和 OutputStreamWriter] ( #inputstreamreader-和-outputstreamwriter )
24+ - [ BufferedReader] ( #bufferedreader )
25+ - [ PrintStream] ( #printstream )
2426- [ FileInputStream 和 FileOutputStream] ( #fileinputstream-和-fileoutputstream )
2527- [ ByteArrayInputStream 和 ByteArrayOutputStream] ( #bytearrayinputstream-和-bytearrayoutputstream )
2628- [ PipedInputStream 和 PipedOutputStream] ( #pipedinputstream-和-pipedoutputstream )
@@ -30,15 +32,13 @@ tags:
3032
3133<!-- /TOC -->
3234
33- ## File 和 RandomAccessFile
34-
35- ### File 类
35+ ## File
3636
3737` File ` 类是 ` java.io ` 包中唯一对文件本身进行操作的类。它可以对文件、目录进行增删查操作。
3838
39- #### 常用方法
39+ ### 常用方法
4040
41- ##### createNewFille
41+ #### createNewFille
4242
4343可以使用 ` createNewFille() ` 方法创建一个新文件。
4444
@@ -57,7 +57,7 @@ File f = new File(filename);
5757boolean flag = f. createNewFile();
5858```
5959
60- ##### mkdir
60+ #### mkdir
6161
6262可以使用 ` mkdir() ` 来创建文件夹,但是如果要创建的目录的父路径不存在,则无法创建成功。
6363
@@ -70,7 +70,7 @@ File f = new File(filename);
7070boolean flag = f. mkdir();
7171```
7272
73- ##### delete
73+ #### delete
7474
7575可以使用 ` delete() ` 来删除文件或目录。
7676
@@ -83,7 +83,7 @@ File f = new File(filename);
8383boolean flag = f. delete();
8484```
8585
86- ##### list 和 listFiles
86+ #### list 和 listFiles
8787
8888` File ` 中给出了两种列出文件夹内容的方法:
8989
@@ -104,7 +104,7 @@ File f = new File(filename);
104104File files[] = f. listFiles();
105105```
106106
107- ### RandomAccessFile 类
107+ ## RandomAccessFile
108108
109109> 注:` RandomAccessFile ` 类虽然可以实现对文件内容的读写操作,但是比较复杂。所以一般操作文件内容往往会使用字节流或字符流方式。
110110
@@ -114,7 +114,7 @@ File files[] = f.listFiles();
114114
115115文件中记录的大小不一定都相同,只要能够确定哪些记录有多大以及它们在文件中的位置即可。
116116
117- #### 写操作
117+ ### 写操作
118118
119119当用 ` rw ` 方式声明 ` RandomAccessFile ` 对象时,如果要写入的文件不存在,系统将自行创建。
120120
@@ -148,7 +148,7 @@ public class RandomAccessFileDemo01 {
148148}
149149```
150150
151- #### 读操作
151+ ### 读操作
152152
153153读取是直接使用 ` r ` 的模式即可,以只读的方式打开文件。
154154
@@ -192,6 +192,116 @@ public class RandomAccessFileDemo02 {
192192}
193193```
194194
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+
195305## 字符流和字节流
196306
197307JAVA IO 中的流操作分为两类:
@@ -307,6 +417,60 @@ public class InputStreamReaderDemo {
307417}
308418```
309419
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+
310474## FileInputStream 和 FileOutputStream
311475
312476** FileInputStream 和 FileOutputStream 用于输入、输出文件。**
0 commit comments