java的字节流和字符流

java的IO流

提示:发现错误,请评论指出,后续修改



提示:以下是本篇文章正文内容,下面案例可供参考

一、字节流和字符流

在Java中,操作文件内容的主要有两大类:字节流和字符流。
字节流的输入输出操作依靠InputStreamOutputStream
字符流的输入输出依靠的是WriterReader类完成
操作流程:

1、使用File类打开一个文件
2、通过自己接流或者字符流的子类,指定输出的位置
3、进行读写操作
4、关闭输入输出流
注意:IO操作属于资源操作,完成操作后一定要关闭

1、字节流

字节输出流

OutputStream是io包中字节输出流的最大父类,当想使用字节输出流的时候,需要使用子类来实例化对象

public static void main(String[] args) throws IOException {
        File file = new File("d:"+File.separator+"test.txt");
        // 只用字节流的子类来实例化输出流对象
        OutputStream ops = new FileOutputStream(file);
        // 进行操作
        String str = "hello OutputStream";
        byte[] bytes = str.getBytes();
        ops.write(bytes);
        // 关闭
        ops.close();
    }

以追加的方式来写,需要再实例化流对象的时候,再后面加上true参数。

public static void main(String[] args) throws IOException {
        File file = new File("d:"+File.separator+"test.txt");
        // 子类实例化对象
        // 在后边添加true,表示追加的方式添加
        OutputStream opt = new FileOutputStream(file,true);
        // 写操作
        String str = "hello OutputStream";
        //换行写入的方式
        // String str = "\r\nhello OutputStream";
        byte[] bytes = str.getBytes();

        for (int number = 0; number < bytes.length; number++) {
            opt.write(bytes[number]);
        }
        opt.close();
    }
字节输入流

字节输入流基本操作与字节输出流一致,实例化也需要使用子类。

public static void main(String[] args) throws IOException {
        File file = new File("d:"+File.separator+"test.txt");
        // 子类实例化对象
        InputStream is = new FileInputStream(file);
        // 读操作
        byte[] bytes = new byte[1024];
        is.read(bytes);
        is.close();
    }

当不知道文件大小的时候,以读取的数据是否为-1作为读取完毕的标识。

public static void main(String[] args) throws IOException{
        File file = new File("d:"+File.separator+"test.txt");
        // 子类实例化对象
        InputStream is = new FileInputStream(file);
        // 读操作,数组大小由文件决定
        byte[] bytes = new byte[(int)file.length()];
        int temp = 0;
        int len = 0;
        while((temp=is.read()) != -1){
            // 如果没读完
            bytes[len] = (byte) temp;
            len++;
        }
        is.close();
    }

2、字符流

在程序中,一个字符=两个字节,针对字符操作的字符输出流Writer和字符输入流Reader。
并且,字符流的Writer和Reader也是接口,只能通过子类来实例化字符流对象。

Writer
public static void main(String[] args) throws IOException {
        File file = new File("d:"+File.separator+"test.txt");
        // 实例化字符输出流对象
        Writer out = new FileWriter(file);
        // 写操作
        String str = "hello Writer";
        out.write(str);
        out.close();
    }

追加的方式写进去

public static void main(String[] args) throws IOException{
        File file = new File("d:"+File.separator+"test.txt");
        // 实例化字符流写对象
        Writer out = new FileWriter(file,true);
        // 写操作
        String str = "\r\nhello Writer";
        out.write(str);
        out.close();
    }
Reader
public static void main(String[] args) throws IOException {
        File file = new File("d:" + File.separator + "test.txt");
        // 实例化字符读对象
        Reader input = new FileReader(file);
        // 读操作
        char[] filechar = new char[1024];
        int len = input.read(filechar);
        // 关闭
        input.close();
    }

不知道文件大小,以读取的数据是否为-1作为读取完毕的标识。

public static void main(String[] args) throws IOException{
        File file = new File("d:" + File.separator + "test.txt");
        // 实例化对象
        Reader input = new FileReader(file);
        // 读操作
        char[] filechar = new char[1024];
        int temp = 0;
        int len = 0;
        while ((temp = input.read())!=-1){
            // 还有内容
            filechar[len] = (char) temp;
            len++;
        }
        input.close();
    }

3、区别

字节流和字符流在缓冲区方面得区别

缓冲区

字节流不适用缓冲区
字符流使用缓冲区

关闭

1、使用字节流,即使流没有关闭,最终也可以输出到文件
2、使用字符流,所有内容保存在缓冲区,关闭流时,会强制性得将缓冲区得内容全部写道文件中,如果没有关闭流,文件中就没有内容写入。

/**
     * 缓冲区刷新
     */
    public static void main(String[] args) throws IOException{
        File file = new File("d:"+File.separator+"test.txt");
        // 实例化对象
        Writer out = new FileWriter(file);
        String str = "\r\nhello write,flush";
        // 写操作
        out.write(str);
        // 刷新缓冲区内容到文件
        out.flush();
        out.close();
    }

案例—文件拷贝

文件拷贝

public static void main(String[] args) throws IOException {
        if (args.length != 2){
            System.out.println("输入的参数不正确");
            System.exit(1);
        }
        File file1 = new File(args[0]);
        File file2 = new File(args[1]);
        InputStream input = new FileInputStream(file1);
        OutputStream out = new FileOutputStream(file2);
        if (input != null && out != null){
            int temp = 0;
            while ((temp=input.read())!= -1){
                out.write(temp);
            }
            System.out.println("拷贝完成");
            input.close();
            out.close();
        }
    }

使用场景

字节流:
------------处理图像,视频,PPT,Word
字符流:
------------纯文本文件,如txt。

注意:字节流可以初六村文本文件,但是字符流不能处理图像视频等文件。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值