JAVAIO整理

来源:互联网 发布:为什么会迷恋网络 编辑:程序博客网 时间:2024/06/08 09:52
IO分字节流和字符流

FileInputStream 用于字节流

InputStreamReader是字节流通向字符流的桥梁

BufferedReader是从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取
通常,Reader 所作的每个读取请求都会导致对底层字符或字节流进行相应的读取请求。因此,建议用 BufferedReader 包装所有其 read() 操作可能开销很高的 Reader(如 FileReader 和 InputStreamReader)


单纯对字节流做操作时,比如复制一个电影,可以用BufferedInputStream来创建一个缓冲区接收字节流

所有缓冲区结束时要用flush()刷新缓冲区(输出缓冲区剩余的数据)

FileReader是用来读取字符文件的便捷类,可以用BufferedReader来创建缓冲区

所有的流用完后都得关闭

注意:BufferedReader只能接受字符流的缓冲区,因为每一个中文需要占据两个字节,所以需要将System.in这个字节输入流变为字符输入流,采用:

BufferedReader buf = new BufferedReader(newInputStreamReader(System.in));


字符流和字节流

字符流的由来: 因为数据编码的不同,而有了对字符进行高效操作的流对象。本质其实就是基于字节流读取时,去查了指定的码表。字节流和字符流的区别:

(1)读写单位不同:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节。

(2)处理对象不同:字节流能处理所有类型的数据(如图片、avi等),而字符流只能处理字符类型的数据。

(3)字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的;而字符流在操作的时候下后是会用到缓冲区的,是通过缓冲区来操作文件,我们将在下面验证这一点。

结论:优先选用字节流。首先因为硬盘上的所有文件都是以字节的形式进行传输或者保存的,包括图片等内容。但是字符只是在内存中才会形成的,所以在开发中,字节流使用广泛。

【案例】DataInputStream类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
importjava.io.DataInputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.IOException;
  
publicclassDataOutputStreamDemo{
   publicstaticvoid main(String[] args) throwsIOException{
       File file = newFile("d:"+ File.separator +"hello.txt");
       DataInputStream input = newDataInputStream(newFileInputStream(file));
       char[] ch = newchar[10];
       intcount = 0;
       chartemp;
       while((temp = input.readChar()) != 'C'){
           ch[count++] = temp;
       }
       System.out.println(ch);
    }
}

【案例】PushBackInputStream回退流操作

?
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
importjava.io.ByteArrayInputStream;
importjava.io.IOException;
importjava.io.PushbackInputStream;
  
/**
 * 回退流操作
 * */
publicclassPushBackInputStreamDemo{
    publicstaticvoid main(String[] args) throwsIOException{
       String str = "hello,rollenholt";
       PushbackInputStream push = null;
       ByteArrayInputStream bat = null;
       bat = newByteArrayInputStream(str.getBytes());
       push = newPushbackInputStream(bat);
       inttemp = 0;
       while((temp = push.read()) != -1){
           if(temp == ','){
                push.unread(temp);
                temp = push.read();
                System.out.print("(回退"+(char) temp + ") ");
           }else{
                System.out.print((char) temp);
           }
       }
    }
}

【案例】向文件中追加新内容

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
 * 字节流
 * 向文件中追加新内容:
 * */
importjava.io.*;
classhello{
   publicstaticvoid main(String[] args) throwsIOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f=newFile(fileName);
       OutputStream out =newFileOutputStream(f,true);//true表示追加模式,否则为覆盖
       String str="Rollen";
       //String str="\r\nRollen"; 可以换行
       byte[] b=str.getBytes();
       for(inti = 0; i < b.length; i++) {
           out.write(b[i]);
       }
       out.close();
    }
}

【案例】验证管道流:进程间通信

?
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
/**
 * 验证管道流
 * */
importjava.io.*;
  
/**
 * 消息发送类
 * */
classSendimplementsRunnable{
   privatePipedOutputStream out=null;
   publicSend() {
       out=newPipedOutputStream();
    }
   publicPipedOutputStream getOut(){
       returnthis.out;
    }
   publicvoidrun(){
       String message="hello , Rollen";
       try{
           out.write(message.getBytes());
       }catch(Exception e) {
           e.printStackTrace();
       }try{
           out.close();
       }catch(Exception e) {
           e.printStackTrace();
       }
    }
}
  
/**
 * 接受消息类
 * */
classReciveimplementsRunnable{
   privatePipedInputStream input=null;
   publicRecive(){
       this.input=newPipedInputStream();
    }
   publicPipedInputStream getInput(){
       returnthis.input;
    }
   publicvoidrun(){
       byte[] b=newbyte[1000];
       intlen=0;
       try{
           len=this.input.read(b);
       }catch(Exception e) {
           e.printStackTrace();
       }try{
           input.close();
       }catch(Exception e) {
           e.printStackTrace();
       }
       System.out.println("接受的内容为 "+(newString(b,0,len)));
    }
}
/**
 * 测试类
 * */
classhello{
   publicstaticvoid main(String[] args) throwsIOException {
       Send send=newSend();
       Recive recive=newRecive();
        try{
//管道连接
           send.getOut().connect(recive.getInput());
       }catch(Exception e) {
           e.printStackTrace();
       }
       newThread(send).start();
       newThread(recive).start();
    }
}

字符流与字节流转换

转换流的特点:

(1)其是字符流和字节流之间的桥梁

(2)可对读取到的字节数据经过指定编码转换成字符

(3)可对读取到的字符数据经过指定编码转换成字节

何时使用转换流?

当字节和字符之间有转换动作时;

流操作的数据需要编码或解码时。

具体的对象体现:

InputStreamReader:字节到字符的桥梁

OutputStreamWriter:字符到字节的桥梁

这两个流对象是字符体系中的成员,它们有转换作用,本身又是字符流,所以在构造的时候需要传入字节流对象进来。


0 0
原创粉丝点击