IO流基本概念
IO流用來(lái)處理設(shè)備之間的數(shù)據(jù)傳輸
Java對(duì)數(shù)據(jù)的操作是通過(guò)流的方式
Java用于操作流的對(duì)象都是在IO包上
流按操作數(shù)據(jù)分為兩種:字節(jié)流和字符流
流按流向分為:輸入流,輸出流。
字節(jié)流的抽象基類:InputStream,OutputStream
字符流的抽象基類:Reader,Writer
注:由這4個(gè)類派生出來(lái)的子類名稱都是以其父類名作為子類名的后綴。
如:InputStream的子類:FileInputStream
如:Reader的子類FileReader
如創(chuàng)建一個(gè)FileWriter對(duì)象,該對(duì)象一被初始化就必須要明確被操作的文件,而且該文件就會(huì)被創(chuàng)建到指定目錄下,如果該目錄下已有同名文件,將被覆蓋。
Demo :
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
|
package javase.day18; import java.io.FileWriter; import java.io.IOException; public class FileWriterDemo { public static void main(String[] args) { FileWriter fw= null ; try { fw = new FileWriter( "C:\\java_test\\FileWriterTest.txt" ); //調(diào)用write 方法,將字符串寫入到流中 fw.write( "alex test23" ); //刷新流對(duì)象中的緩沖中的數(shù)據(jù) fw.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fw!= null ){ //關(guān)閉流資源,但是關(guān)閉之前會(huì)刷新一次內(nèi)部的緩沖中的數(shù)據(jù) fw.close(); } } catch (IOException e) { e.printStackTrace(); } } } } package javase.day18; import java.io.FileWriter; import java.io.IOException; public class FileWriterDemo { public static void main(String[] args) { FileWriter fw= null ; try { fw = new FileWriter( "C:\\java_test\\FileWriterTest.txt" ); //調(diào)用write 方法,將字符串寫入到流中 fw.write( "alex test23" ); //刷新流對(duì)象中的緩沖中的數(shù)據(jù) fw.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fw!= null ){ //關(guān)閉流資源,但是關(guān)閉之前會(huì)刷新一次內(nèi)部的緩沖中的數(shù)據(jù) fw.close(); } } catch (IOException e) { e.printStackTrace(); } } } } |
打印Java文件的源代碼Demo code:
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
|
package javase.day18; import java.io.FileReader; import java.io.IOException; public class FileReaderTest { public static void main(String[] args) { try { FileReader fr= new FileReader( "C:\\java_test\\SystemDemo.java" ); char [] buf= new char [ 1024 ]; int num= 0 ; while ((num=fr.read(buf))!=- 1 ){ System.out.println( new String(buf, 0 ,num)); } } catch (IOException e) { e.printStackTrace(); } } } package javase.day18; import java.io.FileReader; import java.io.IOException; public class FileReaderTest { public static void main(String[] args) { try { FileReader fr= new FileReader( "C:\\java_test\\SystemDemo.java" ); char [] buf= new char [ 1024 ]; int num= 0 ; while ((num=fr.read(buf))!=- 1 ){ System.out.println( new String(buf, 0 ,num)); } } catch (IOException e) { e.printStackTrace(); } } } |
復(fù)制文件Demo code:
copy_1() 使用的方法是讀取一個(gè)字符則寫入一個(gè)字符。
copy_2()使用的方法是把字符一次性讀取到一個(gè)字符數(shù)組中,最后再一次寫入到目標(biā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
|
package javase.day18; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class CopyText { public static void main(String[] args) { try { copy_1(); } catch (IOException e) { e.printStackTrace(); } } public static void copy_1() throws IOException{ FileWriter fw = new FileWriter( "C:\\java_test\\Copy_SystemDemo.java" ); FileReader fr = new FileReader( "C:\\java_test\\SystemDemo.java" ); int num= 0 ; while ((num=fr.read())!=- 1 ){ fw.write(num); } fw.close(); fr.close(); } public static void copy_2() throws IOException{ FileWriter fw = new FileWriter( "C:\\java_test\\Copy_SystemDemo.java" ); FileReader fr = new FileReader( "C:\\java_test\\SystemDemo.java" ); int num= 0 ; char [] buf= new char [ 1024 ]; while ((num=fr.read(buf))!=- 1 ){ fw.write(buf, 0 ,num); } fw.close(); fr.close(); } } package javase.day18; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class CopyText { public static void main(String[] args) { try { copy_1(); } catch (IOException e) { e.printStackTrace(); } } public static void copy_1() throws IOException{ FileWriter fw = new FileWriter( "C:\\java_test\\Copy_SystemDemo.java" ); FileReader fr = new FileReader( "C:\\java_test\\SystemDemo.java" ); int num= 0 ; while ((num=fr.read())!=- 1 ){ fw.write(num); } fw.close(); fr.close(); } public static void copy_2() throws IOException{ FileWriter fw = new FileWriter( "C:\\java_test\\Copy_SystemDemo.java" ); FileReader fr = new FileReader( "C:\\java_test\\SystemDemo.java" ); int num= 0 ; char [] buf= new char [ 1024 ]; while ((num=fr.read(buf))!=- 1 ){ fw.write(buf, 0 ,num); } fw.close(); fr.close(); } } |
字符流的緩沖區(qū):
緩沖區(qū)的出現(xiàn)提高了對(duì)數(shù)據(jù)的讀寫效率。
對(duì)應(yīng)類:BufferedWriter , BufferedReader .
緩沖區(qū)要結(jié)合流才可以使用。
在流的基礎(chǔ)上對(duì)流的功能進(jìn)行了增強(qiáng)。
IO流操作的基本規(guī)律:
1,明確源和目的:
源: 輸入流 InputStream , Reader
目的: 輸出流 OutputStream ,Writer
2,操作的數(shù)據(jù)是否是純文本:
是:字符流
否:字節(jié)流
即:(1) 當(dāng)為輸入字符流用Reader
(2) 當(dāng)為輸入字節(jié)流用InputStream
(3)當(dāng)為輸出字符流用Writer
(4)當(dāng)為輸出字節(jié)流用OutputStream
3,當(dāng)體系明確后,再明確要使用哪個(gè)具體的對(duì)象:
源設(shè)備:內(nèi)存,硬盤,鍵盤
目的設(shè)備:內(nèi)存,硬盤,控制臺(tái)
IO操作工具類
[1] String fileReaderStringHandle(String fileName)
將文件(由fileName指定)讀入到一個(gè)字符串;
[2] byte[] fileReaderByteHandle(String fileName)
將文件(由fileName指定)讀入到一個(gè)字節(jié)數(shù)組;
[3] void fileWriterHandle(String fileName, String text)
將字符串(由text指定)寫出到一個(gè)文件(由fileName指定)。
IOUtil.java
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
|
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; public class IOUtil { /** * 將文件讀入到一個(gè)String,利用FileReader+BufferedReader(提供readLine方法) * * @param fileName * @return String */ public static String fileReaderStringHandle(String fileName) { StringBuilder sb = new StringBuilder(); try { BufferedReader in = new BufferedReader( new FileReader( new File( fileName).getAbsoluteFile())); try { String s; while ((s = in.readLine()) != null ) { sb.append(s); sb.append( "\n" ); } } finally { in.close(); } } catch (IOException e) { throw new RuntimeException(e); } return sb.toString(); } /** * 使用FileInputStream+BufferedInputStream以byte的方式處理文件 * * @param fileName * @return byte[] */ public static byte [] fileReaderByteHandle(String fileName) { byte [] data = null ; try { BufferedInputStream bf = new BufferedInputStream( new FileInputStream(fileName)); try { data = new byte [bf.available()]; bf.read(data); } finally { bf.close(); } } catch (IOException e) { throw new RuntimeException(e); } return data == null ? new byte [] {} : data; } /** * 將指定的text寫入到文件名為fileName的文件中 * * @param fileName * @param text */ public static void fileWriterHandle(String fileName, String text) { try { PrintWriter out = new PrintWriter( new File(fileName) .getAbsoluteFile()); try { out.print(text); } finally { out.close(); } } catch (IOException e) { throw new RuntimeException(e); } } public static void main(String[] args) throws IOException { System.out.print(fileReaderStringHandle( "src/IOUtil.java" )); for ( byte b : fileReaderByteHandle( "src/IOUtil.java" )) System.out.print(b); fileWriterHandle( "zj.txt" , fileReaderStringHandle( "src/IOUtil.java" )); } } |