字节流
IO流(输入流、输出流)
字节流、字符流
1.字节流
1)InputStream、OutputStream
InputStream抽象了应用程序读取数据的方式
OutputStream抽象了应用程序写出数据的方式
2)EOF = End 读到-1就读到结尾
3)输入流基本方法
int b = in.read();读取一个字节无符号填充到int低八位.-1是 EOF
in.read(byte[] buf)
in.read(byte[] buf,int start,int size)
4)输出流基本方法
out.write(int b) 写出一个byte到流,b的低8位
out.write(byte[] buf)将buf字节数组都写入到流
out.write(byte[] buf,int start,int size)
5)FileInputStream--->具体实现了在文件上读取数据
6)FileOutputStream 实现了向文件中写出byte数据的方法
7)DataOutputStream/DataInputStream
对"流"功能的扩展,可以更加方面的读取int,long,字符等类型数据
DataOutputStream
writeInt()/writeDouble()/writeUTF()
8)BufferedInputStream&BufferedOutputStream
这两个流类位IO提供了带缓冲区的操作,一般打开文件进行写入
或读取操作时,都会加上缓冲,这种流模式提高了IO的性能
从应用程序中把输入放入文件,相当于将一缸水倒入到另一个缸中:
FileOutputStream--->write()方法相当于一滴一滴地把水“转移”过去
DataOutputStream-->writeXxx()方法会方便一些,相当于一瓢一瓢把水“转移”过去
BufferedOutputStream--->write方法更方便,相当于一飘一瓢先放入桶中,再从桶中倒入到另一个缸中,性能提高了
1 import java.io.BufferedInputStream;
2 import java.io.BufferedOutputStream;
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7
8 public class IOUtil {
9 /**
10 * 读取指定文件内容,按照16进制输出到控制台
11 * 并且每输出10个byte换行
12 * @param fileName
13 * 单字节读取不适合大文件,大文件效率很低
14 */
15 public static void printHex(String fileName)throws IOException{
16 //把文件作为字节流进行读操作
17 FileInputStream in = new FileInputStream(fileName);
18 int b ;
19 int i = 1;
20 while((b = in.read())!=-1){
21 if(b <= 0xf){
22 //单位数前面补0
23 System.out.print("0");
24 }
25 System.out.print(Integer.toHexString(b)+" ");
26 if(i++%10==0){
27 System.out.println();
28 }
29 }
30 in.close();
31 }
32 /**
33 * 批量读取,对大文件而言效率高,也是我们最常用的读文件的方式
34 * @param fileName
35 * @throws IOException
36 */
37 public static void printHexByByteArray(String fileName)throws IOException{
38 FileInputStream in = new FileInputStream(fileName);
39 byte[] buf = new byte[8 * 1024];
40 /*从in中批量读取字节,放入到buf这个字节数组中,
41 * 从第0个位置开始放,最多放buf.length个
42 * 返回的是读到的字节的个数
43 */
44 /*int bytes = in.read(buf,0,buf.length);//一次性读完,说明字节数组足够大
45 int j = 1;
46 for(int i = 0; i < bytes;i++){
47 System.out.print(Integer.toHexString(buf[i] & 0xff)+" ");
48 if(j++%10==0){
49 System.out.println();
50 }
51 }*/
52 int bytes = 0;
53 int j = 1;
54 while((bytes = in.read(buf,0,buf.length))!=-1){
55 for(int i = 0 ; i < bytes;i++){
56 System.out.print(Integer.toHexString(buf[i] & 0xff)+" ");
57 if(j++%10==0){
58 System.out.println();
59 }
60 }
61 }
62 in.close();
63 }
64 /**
65 * 文件拷贝,字节批量读取
66 * @param srcFile
67 * @param destFile
68 * @throws IOException
69 */
70 public static void copyFile(File srcFile,File destFile)throws IOException{
71 if(!srcFile.exists()){
72 throw new IllegalArgumentException("文件:"+srcFile+"不存在");
73 }
74 if(!srcFile.isFile()){
75 throw new IllegalArgumentException(srcFile+"不是文件");
76 }
77 FileInputStream in = new FileInputStream(srcFile);
78 FileOutputStream out = new FileOutputStream(destFile);
79 byte[] buf = new byte[8*1024];
80 int b ;
81 while((b = in.read(buf,0,buf.length))!=-1){
82 out.write(buf,0,b);
83 out.flush();//最好加上
84 }
85 in.close();
86 out.close();
87
88 }
89 /**
90 * 进行文件的拷贝,利用带缓冲的字节流
91 * @param srcFile
92 * @param destFile
93 * @throws IOException
94 */
95 public static void copyFileByBuffer(File srcFile,File destFile)throws IOException{
96 if(!srcFile.exists()){
97 throw new IllegalArgumentException("文件:"+srcFile+"不存在");
98 }
99 if(!srcFile.isFile()){
100 throw new IllegalArgumentException(srcFile+"不是文件");
101 }
102 BufferedInputStream bis = new BufferedInputStream(
103 new FileInputStream(srcFile));
104 BufferedOutputStream bos = new BufferedOutputStream(
105 new FileOutputStream(destFile));
106 int c ;
107 while((c = bis.read())!=-1){
108 bos.write(c);
109 bos.flush();//刷新缓冲区
110 }
111 bis.close();
112 bos.close();
113 }
114 /**
115 * 单字节,不带缓冲进行文件拷贝
116 * @param srcFile
117 * @param destFile
118 * @throws IOException
119 */
120 public static void copyFileByByte(File srcFile,File destFile)throws IOException{
121 if(!srcFile.exists()){
122 throw new IllegalArgumentException("文件:"+srcFile+"不存在");
123 }
124 if(!srcFile.isFile()){
125 throw new IllegalArgumentException(srcFile+"不是文件");
126 }
127 FileInputStream in = new FileInputStream(srcFile);
128 FileOutputStream out = new FileOutputStream(destFile);
129 int c ;
130 while((c = in.read())!=-1){
131 out.write(c);
132 out.flush();
133 }
134 in.close();
135 out.close();
136 }
137
138 }
415

被折叠的 条评论
为什么被折叠?



