JAVAIO整理

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
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
  
public class DataOutputStreamDemo{
    public static void main(String[] args) throws IOException{
        File file = new File( "d:" + File.separator + "hello.txt" );
        DataInputStream input = new DataInputStream( new FileInputStream(file));
        char [] ch = new char [ 10 ];
        int count = 0 ;
        char temp;
        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
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PushbackInputStream;
  
/**
  * 回退流操作
  * */
public class PushBackInputStreamDemo{
     public static void main(String[] args) throwsIOException{
        String str = "hello,rollenholt" ;
        PushbackInputStream push = null ;
        ByteArrayInputStream bat = null ;
        bat = new ByteArrayInputStream(str.getBytes());
        push = new PushbackInputStream(bat);
        int temp = 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
/**
  * 字节流
  * 向文件中追加新内容:
  * */
import java.io.*;
class hello{
    public static void main(String[] args) throws IOException {
        String fileName= "D:" +File.separator+ "hello.txt" ;
        File f= new File(fileName);
        OutputStream out = new FileOutputStream(f, true ); //true表示追加模式,否则为覆盖
        String str= "Rollen" ;
        //String str="\r\nRollen"; 可以换行
        byte [] b=str.getBytes();
        for ( int i = 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
/**
  * 验证管道流
  * */
import java.io.*;
  
/**
  * 消息发送类
  * */
class Send implements Runnable{
    private PipedOutputStream out= null ;
    public Send() {
        out= new PipedOutputStream();
     }
    public PipedOutputStream getOut(){
        return this .out;
     }
    public void run(){
        String message= "hello , Rollen" ;
        try {
            out.write(message.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } try {
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
     }
}
  
/**
  * 接受消息类
  * */
class Recive implements Runnable{
    private PipedInputStream input= null ;
    public Recive(){
        this .input= new PipedInputStream();
     }
    public PipedInputStream getInput(){
        return this .input;
     }
    public void run(){
        byte [] b= new byte [ 1000 ];
        int len= 0 ;
        try {
            len= this .input.read(b);
        } catch (Exception e) {
            e.printStackTrace();
        } try {
            input.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println( "接受的内容为 " +( new String(b, 0 ,len)));
     }
}
/**
  * 测试类
  * */
class hello{
    public static void main(String[] args) throws IOException {
        Send send= new Send();
        Recive recive= new Recive();
         try {
//管道连接
            send.getOut().connect(recive.getInput());
        } catch (Exception e) {
            e.printStackTrace();
        }
        new Thread(send).start();
        new Thread(recive).start();
     }
}

字符流与字节流转换

转换流的特点:

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

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

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

何时使用转换流?

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

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

具体的对象体现:

InputStreamReader:字节到字符的桥梁

OutputStreamWriter:字符到字节的桥梁

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值