java --字节流 和 字符流的相关使用方法

本文详细介绍了Java中的IO流操作,包括字节流和字符流的基本使用方法,如读写文件、复制图片和音频文件等。并通过多种实例展示了如何提高文件读写的效率。
    //字节流: INputStream/OutputStream
    //字符流: Reader/Writer

    static String path = "路径";

    public static void main(String[] args) throws IOException {
//        demo1();
//        demo2();
//        demo3();
//        demo4();
//        demo5();
//        demo6();
//        demo7();
//        demo8();
//        demo9();
//        demo10();
//        demo11();
//        demo12();
//        demo13();
//        demo14();
        demo15();
    }

    /*******************************
     * 字节流
     ******************************/

    //读
    public static void demo1() throws IOException {

        //对文件操作的类
        //参数是文件的路径
        File file = new File("路径/xxx.txt");
        //选择输入的字节流进行操作
        FileInputStream fileInputStream = new FileInputStream(file);
        //读内容
        int x = fileInputStream.read();
        System.out.println(x);
        int y = fileInputStream.read();
        System.out.println(y);
        int z = fileInputStream.read();
        System.out.println(z);
        int t = fileInputStream.read();
        System.out.println(t);
        //关闭流
        fileInputStream.close();
    }

    public static void demo2() throws IOException {
        //对文件操作的类
        //参数是文件的路径
        File file = new File("路径/xxx.txt");
        //选择输入的字节流进行操作
        FileInputStream fileInputStream = new FileInputStream(file);
        //读内容
        int x = 0;
        while ((x = fileInputStream.read()) != -1) {
            System.out.println(x);
        }
        fileInputStream.close();
    }

    //写
    public static void demo3() throws IOException {

        File file = new File("路径/yyy.txt");
        //若有文件,就覆盖内容
        //若没有文件,新建文件夹并写入
//       FileOutputStream fileOutputStream = new FileOutputStream(file);
        //若要追加内容,需要使用重载方法,第二个参数true
        FileOutputStream fileOutputStream = new FileOutputStream(file, true);

        fileOutputStream.write(100);
        fileOutputStream.write(101);
        fileOutputStream.write(102);

        fileOutputStream.close();
    }

    //图片拷贝
    public static void demo4() throws IOException {
        FileInputStream fileInputStream = new FileInputStream(path + "1.jpg");
        FileOutputStream fileOutputStream = new FileOutputStream(path + "copy_1.jpg");
        int b = 0;
        while ((b = fileInputStream.read()) != 0) {
            fileOutputStream.write(b);
        }
        fileInputStream.close();
        fileOutputStream.close();
    }

    //MP3拷贝
    public static void demo5() throws IOException {

        FileInputStream fileInputStream = new FileInputStream(path + "whzg.mp3");
        FileOutputStream fileOutputStream = new FileOutputStream(path + "copy_whzg.mp3");
        int b = 0;
        while ((b = fileInputStream.read()) != 0) {
            fileOutputStream.write(b);
        }
        fileInputStream.close();
        fileOutputStream.close();
    }

    //大数组的方式拷贝
    //弊端 - 被拷贝文件太大的时候,会造成内存溢出
    public static void demo6() throws IOException {
        FileInputStream fileInputStream = new FileInputStream(path + "whzg.mp3");
        FileOutputStream fileOutputStream = new FileOutputStream(path + "new_whzg.mp3");
        //File类中 - file.length()获取文件的长度
        //fileInputStream.available();不阻塞情况下,可读取的最大长度
        int len = fileInputStream.available();
        System.out.println(len);
        //缓存区 - 鸡蛋蓝
        byte[] arr = new byte[len];

        //将文件内容读到一个arr中
        fileInputStream.read(arr);
        //将arr中的内容写入到文件夹中
        fileOutputStream.write(arr);

        fileInputStream.close();
        fileOutputStream.close();

    }

    //小数组的拷贝方式
    //注意: 小心缓存数组中存储着上一次读取的部分内容
    public static void demo7() throws IOException {
        FileInputStream fileInputStream = new FileInputStream(path + "xxx.txt");
        FileOutputStream fileOutputStream = new FileOutputStream(path + "zzz.txt");
        //缓存区 - 鸡蛋蓝
        byte[] arr = new byte[1024 * 8];

//        //将文件内容读到一个arr中
//        fileInputStream.read(arr);
//
//        for (int i = 0; i < arr.length; i++) {
//            System.out.println(arr[i]);
//        }
//
//        fileInputStream.read(arr);
//        for (int i = 0; i < arr.length; i++) {
//            System.out.println(arr[i]);
//        }
        int len1 = 0;
        //read (arr) -> 将内容读到数组中
        //返回值 : 当有内容读时,返回每次实际读取到的字节数
        //        当无内容课读时,返回-1
        while ((len1 = fileInputStream.read(arr)) != -1) {
            //将arr中的内容写入到文件中
//            fileOutputStream.write(arr);
            //将参数1中的内容写入到文件中
            //从数组的第参数2个的位置开始,写入参数3的长度
            fileOutputStream.write(arr, 0, len1);
        }
        //将arr中的内容写入到文件夹中
//        fileOutputStream.write(arr);
        fileInputStream.close();
        fileOutputStream.close();

    }

    //系统自带的缓冲
    public static void demo8() throws IOException {
        FileInputStream fileInputStream = new FileInputStream(path + "xxx.txt");
        FileOutputStream fileOutputStream = new FileOutputStream(path + "zzz.txt");
        //将fileInputStream包装一下,使其带有缓存区
        BufferedInputStream bis = new BufferedInputStream(fileInputStream);
        //将fileOutputStream包装一下,使其带有缓存区
        BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
        int b = 0;
        while ((b = bis.read()) != 0) {
            bos.write(b);
        }
        //flush 刷新
        //close刷新并关闭流
        //将缓存区的内容刷新进文件
//        bos.flush();
//        bos.write(100);
//        bos.flush();

        //注意:不需要再关闭fileInputStream和fileOutputStream
        //因为bis和bos 就是对其进行了包装
        bis.close();
        bos.close();


    }

    //字节流读取中文字符
    public static void demo9() throws IOException {

        FileInputStream fileInputStream = new FileInputStream(path + "xxx.txt");
        //一个中文字符由2/3/n个字节组成
        //因此写个缓存,一次读出对应的字节数
        byte[] arr = new byte[3];
        int b = 0;
        while ((b = fileInputStream.read(arr)) != -1) {
            //将字节还原成中文字符,再输出
            System.out.print(new String(arr, 0, b));
        }
        fileInputStream.close();
    }

    //字节流写中文字符
    public static void demo10() throws IOException {
        FileOutputStream fos = new FileOutputStream(path + "yyy.txt");
        //写入内容
        fos.write("你好,你好!!!".getBytes());
        //写入换行
        fos.write("\r\n".getBytes());
        fos.close();
    }

    /********************************************
     * 字符流
     *************************************/
    //读
    public static void demo11() throws IOException {
        File flie = new File(path + "xxx.txt");
        FileReader fr = new FileReader(flie);
        int c = 0;
        while ((c = fr.read()) != -1) {
            System.out.print((char) c);
        }
        fr.close();
    }

    //写
    public static void demo12() throws IOException {

        File flie = new File(path + "xxx.txt");
        FileWriter fw = new FileWriter(flie);
        fw.write("大家好!!");
        fw.write(97);
        fw.close();
    }

    //字符流不能拷贝非纯文本的文件,因为会将不识别的字符读成?再写到新文本中
    //拷贝

    //也不推荐使用字符流拷贝纯文本文件,因为读和写都需要多一步转码的过程

    //只有直接读出字符内容的文档或者单纯写入时才推荐使用字符流
    public static void demo13() throws IOException {

        FileReader fr = new FileReader(path + "xxx.txt");
        FileWriter fw = new FileWriter(path + "ccc.txt");
        int c = 0;
        while ((c = fr.read()) != -1) {
            fw.write(c);
        }
        fr.close();
        //注意:Writer类中有一个2k的小缓冲区,若不关流
        //    会将内容写到缓冲区,关闭流后才刷新到硬盘上
        fw.close();
    }


    //自带缓冲区的拷贝
    public static void demo14() throws IOException {
        FileReader fr = new FileReader(path + "xxx.txt");
        FileWriter fw = new FileWriter(path + "ccc.txt");

        //将fr 和 br 包装到缓冲区
        BufferedReader br = new BufferedReader(fr);
        BufferedWriter bw = new BufferedWriter(fw);

        int c = 0;
        while ((c = br.read()) != -1) {
            bw.write(c);
            //"\r\n"与newLine都是换行的作用
            //newLine的兼容性更高,可跨平台
            bw.newLine();
        }
        br.close();
        bw.close();
    }

    //readline() - 按行读
    public static void demo15() throws IOException {

        FileReader fr = new FileReader(path + "xxx.txt");
        BufferedReader br = new BufferedReader(fr);
        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
    }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值