IO流(輸入流、輸出流),又分為字節流、字符流。
流是磁盤或其它外圍設備中存儲的數據的源點或終點。
輸入流:程序從輸入流讀取數據源。數據源包括外界(鍵盤、文件、網絡…),即是將數據源讀入到程序的通信通道。
輸出流:程序向輸出流寫入數據。將程序中的數據輸出到外界(顯示器、打印機、文件、網絡…)的通信通道。
字節流
1.InputStream、OutputStream
InputStream抽象了應用程序讀取數據的方式
OutputStream抽象了應用程序寫出數據的方式
2.讀到文件結尾,稱為EOF = end,讀到-1就讀到結尾
3.輸入流基本方法
int b = in.read();讀取一個字節,無符號填充到int的低八位.-1是EOF
int.read(byte[] buf)讀取數據填充到字節數組buf
int.read(byte[] buf, int start, int size)讀取數據填充到字節數組buf,從buf的start位置開始存儲size長度的數據
4.輸出流基本方法
out.write(int b);寫出一個byte到流,b的低八位
out.write(byte[] buf);將buf字節數組都寫入到流
out.write(byte[] buf, int start, int size);字節數組buf從start位置開始寫size長度的字節到流
5.FileInputStream是InputStream的子類,具體實現了在文件上讀取數據
6.FileOutputStream是OutputStream的子類,實現了向文件中寫出字節數據的方法
FileInputStream的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
60
61
62
63
64
65
66
67
68
69
70
71
|
package com.test.io; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class IOUtils { /** * 讀取指定文件內容,按照十六進制輸出到控制臺 * 并且每輸出10個byte換行 * @param fileName * @throws IOException */ public static void printHex(String fileName) throws IOException { //把文件作為字節流進行讀操作 FileInputStream in = new FileInputStream(fileName); int b; int i = 1 ; while ((b = in.read()) != - 1 ) { if (b <= 0xf ) { System.out.print( "0" ); } System.out.print(Integer.toHexString(b) + " " ); if (i % 10 == 0 ) { System.out.println( "" ); } i++; } in.close(); } public static void printHexByByteArray(String fileName) throws IOException { FileInputStream in = new FileInputStream(fileName); byte [] buf = new byte [ 20 * 1024 ]; //如果字節數組夠大,可以一次性讀完 //從in中批量讀取字節,放入到buf這個字節數組中,從第0個位置開始放,最多放buf.length個,返回的是讀到的字節的個數 /* int bytes = in.read(buf, 0, buf.length); int j = 1; for(int i = 0;i < bytes; i++) { if (buf[i] <= 0xf) { System.out.print("0"); } System.out.print(Integer.toHexString(buf[i] & 0xff) + " "); if (j % 10 == 0) { System.out.println(""); } j++; } */ //如果字節數組不夠大,不能一次性讀完 int bytes = 0 ; int j = 1 ; while ((bytes = in.read(buf, 0 , buf.length)) != - 1 ) { for ( int i = 0 ; i <bytes; i++) { if (buf[i] <= 0xf ) { System.out.print( "0" ); } System.out.print(Integer.toHexString(buf[i] & 0xff ) + " " ); if (j % 10 == 0 ) { System.out.println( "" ); } j++; } } } } |
FileOutputStream的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
|
package com.test.io; import java.io.FileOutputStream; import java.io.IOException; public class FileOutputDemo { public static void main(String[] args) throws IOException { //如果該文件不存在,則直接創建,如果存在,刪除后創建。(如果第二個參數為 true,則將字節寫入文件末尾處,而不是寫入文件開始處。) FileOutputStream out = new FileOutputStream( "F:\\javaio\\out.dat" ); out.write( 'A' ); //寫入了‘A'的低八位(一次只寫入一個字節) int a = 10 ; out.write(a >>> 24 ); out.write(a >>> 16 ); out.write(a >>> 8 ); out.write(a); byte [] b = "10" .getBytes(); out.write(b); out.close(); IOUtils.printHex( "F:\\javaio\\out.dat" ); } } |
7.DataOutputStream和DataInputStream,對流功能的擴展,可以更加方便的讀取int,long,字符等類型數據。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.test.io; import java.io.DataOutputStream; import java.io.FileOutputStream; import java.io.IOException; public class DataOutputDemo { public static void main(String[] args) throws IOException { String file = "F:\\javaio\\b.txt" ; DataOutputStream dos = new DataOutputStream( new FileOutputStream(file)); dos.writeInt( 10 ); dos.writeInt(- 10 ); dos.writeLong(10l); dos.writeDouble( 10.5 ); dos.writeUTF( "你好" ); dos.writeChars( "中國" ); dos.close(); IOUtils.printHex(file); } } |
運行結果:
1
2
3
4
|
00 00 00 0a ff ff ff f6 00 00 00 00 00 00 00 0a 40 25 00 00 00 00 00 00 00 06 e4 bd a0 e5 a5 bd 4e 2d 56 fd |
其中,00 06兩個字節是“你好”這兩個中文的字節個數。
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
|
package com.test.io; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; public class DataInputDemo { public static void main(String[] args) throws IOException { String file = "F:\\javaio\\b.txt" ; DataInputStream dis = new DataInputStream( new FileInputStream(file)); int i = dis.readInt(); System.out.println(i); i = dis.readInt(); System.out.println(i); long l = dis.readLong(); System.out.println(l); double d = dis.readDouble(); System.out.println(d); String s = dis.readUTF(); System.out.println(s); dis.close(); } } |
運行結果:
1
2
3
4
5
|
10 - 10 10 10.5 你好 |
8.BufferedInputStream&BufferedOutputStream,這兩個流類為IO提供了帶緩沖區的操作,一般打開文件進行寫入或讀取操作時,都會加上緩沖,這種流模式提高了IO的性能。
文件的拷貝:
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
|
package com.test.io; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class IOUtils { /** * 拷貝文件,字節批量讀取 * @param srcFile 源文件 * @param destFile 目標文件 * @throws IOException */ public static void copyFile(File srcFile, File destFile) throws IOException { if (!srcFile.exists()) { throw new IllegalArgumentException( "文件" + srcFile + "不存在" ); } if (!srcFile.isFile()) { throw new IllegalArgumentException(srcFile + "不是文件" ); } FileInputStream in = new FileInputStream(srcFile); FileOutputStream out = new FileOutputStream(destFile); byte [] buf = new byte [ 10 * 1024 ]; int b; while ((b = in.read(buf, 0 , buf.length)) != - 1 ) { out.write(buf, 0 ,b); out.flush(); //最好加上,刷新此輸出流并強制寫出所有緩沖的輸出字節。 } in.close(); out.close(); } /** * 拷貝文件,利用帶緩沖的字節流 * @param srcFile * @param destFile * @throws IOException */ public static void copyFileByBuffer(File srcFile, File destFile) throws IOException { if (!srcFile.exists()) { throw new IllegalArgumentException( "文件" + srcFile + "不存在" ); } if (!srcFile.isFile()) { throw new IllegalArgumentException(srcFile + "不是文件" ); } FileInputStream in = new FileInputStream(srcFile); FileOutputStream out = new FileOutputStream(destFile); BufferedInputStream bis = new BufferedInputStream(in); BufferedOutputStream bos = new BufferedOutputStream(out); int c; while ((c = bis.read()) != - 1 ) { bos.write(c); bos.flush(); } bis.close(); bos.close(); } /** * 拷貝文件,通過單字節讀取 * @param srcFile * @param destFile * @throws IOException */ public static void copyFileByByte(File srcFile, File destFile) throws IOException { if (!srcFile.exists()) { throw new IllegalArgumentException( "文件" + srcFile + "不存在" ); } if (!srcFile.isFile()) { throw new IllegalArgumentException(srcFile + "不是文件" ); } FileInputStream in = new FileInputStream(srcFile); FileOutputStream out = new FileOutputStream(destFile); int c; while ((c = in.read()) != - 1 ) { out.write(c); out.flush(); } in.close(); out.close(); } } |
測試文件拷貝:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.test.io; import java.io.File; import java.io.IOException; public class IOUtilsTest { public static void main(String[] args) { //IOUtils.printHex("D:\\javaProgram\\Hello.java"); try { long start = System.currentTimeMillis(); //IOUtils.copyFile(new File("F:\\javaio\\1.mp3"), new File("F:\\javaio\\2.mp3"));//211ms //IOUtils.copyFileByBuffer(new File("F:\\javaio\\1.mp3"), new File("F:\\javaio\\3.mp3"));//18583ms IOUtils.copyFileByByte( new File( "F:\\javaio\\1.mp3" ), new File( "F:\\javaio\\4.mp3" )); //37822ms long end = System.currentTimeMillis(); System.out.println(end - start); } catch (IOException e) { e.printStackTrace(); } } } |
根據以上測試看出,文件拷貝,最快的方式是通過字節的批量讀取。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://www.cnblogs.com/tianxintian22/p/6821267.html