IO流基本概念
IO流用來處理設備之間的數據傳輸
Java對數據的操作是通過流的方式
Java用于操作流的對象都是在IO包上
流按操作數據分為兩種:字節流和字符流
流按流向分為:輸入流,輸出流。
字節流的抽象基類:InputStream,OutputStream
字符流的抽象基類:Reader,Writer
注:由這4個類派生出來的子類名稱都是以其父類名作為子類名的后綴。
如:InputStream的子類:FileInputStream
如:Reader的子類FileReader
如創建一個FileWriter對象,該對象一被初始化就必須要明確被操作的文件,而且該文件就會被創建到指定目錄下,如果該目錄下已有同名文件,將被覆蓋。
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" ); //調用write 方法,將字符串寫入到流中 fw.write( "alex test23" ); //刷新流對象中的緩沖中的數據 fw.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fw!= null ){ //關閉流資源,但是關閉之前會刷新一次內部的緩沖中的數據 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" ); //調用write 方法,將字符串寫入到流中 fw.write( "alex test23" ); //刷新流對象中的緩沖中的數據 fw.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fw!= null ){ //關閉流資源,但是關閉之前會刷新一次內部的緩沖中的數據 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(); } } } |
復制文件Demo code:
copy_1() 使用的方法是讀取一個字符則寫入一個字符。
copy_2()使用的方法是把字符一次性讀取到一個字符數組中,最后再一次寫入到目標文件。
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(); } } |
字符流的緩沖區:
緩沖區的出現提高了對數據的讀寫效率。
對應類:BufferedWriter , BufferedReader .
緩沖區要結合流才可以使用。
在流的基礎上對流的功能進行了增強。
IO流操作的基本規律:
1,明確源和目的:
源: 輸入流 InputStream , Reader
目的: 輸出流 OutputStream ,Writer
2,操作的數據是否是純文本:
是:字符流
否:字節流
即:(1) 當為輸入字符流用Reader
(2) 當為輸入字節流用InputStream
(3)當為輸出字符流用Writer
(4)當為輸出字節流用OutputStream
3,當體系明確后,再明確要使用哪個具體的對象:
源設備:內存,硬盤,鍵盤
目的設備:內存,硬盤,控制臺
IO操作工具類
[1] String fileReaderStringHandle(String fileName)
將文件(由fileName指定)讀入到一個字符串;
[2] byte[] fileReaderByteHandle(String fileName)
將文件(由fileName指定)讀入到一個字節數組;
[3] void fileWriterHandle(String fileName, String text)
將字符串(由text指定)寫出到一個文件(由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 { /** * 將文件讀入到一個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" )); } } |