IO流

IO流的体系结构

在这里插入图片描述
在这里插入图片描述

FileReader和FileWriter

FileReader读入数据的基本操作

/*
    *
    将IO下的hello.txt文件内容读入程序中,并输出到控制台
    说明点:
    1. read()的理解:返回读入的一个字符。如果达到文件末尾,返回-1
    2. 异常的处理:为了保证流资源一定可以执行关闭操作。需要使用try-catch-finally处理
    3. 读入的文件一定要存在,否则就会报FileNotFoundException。*/
    @Test
    public void testFileReader(){
        FileReader fr = null;
        try {
            //1.实例化File类的对象,指明要操作的文件
            File file = new File("hello.txt");//相较于当前Moudle
            //2.提供具体的流
            fr = new FileReader(file);
            //3.数据读入
            //read():返回读入的一个字符。如果达到文件末尾,返回-1
            //方式一:
            int data = fr.read();
            while(data != -1){
                System.out.print((char) data);
                data = fr.read();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(fr != null){
                    //4.关闭流
                    fr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //E:\java\javaSE\IO\src\com\pan\java1\FileReaderWriterTest.java

FileReader中使用read(char [] cbuf)读入数据

@Test
    public void testFileReader1() throws IOException {
        FileReader fr = null;
        try {
            File file = new File("hello.txt");
            fr = new FileReader(file);
            char[] chars = new char[5];
            int len;
            while((len = fr.read(chars)) != -1){
                for (int i = 0; i < len; i++) {
                    System.out.print(chars[i]);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fr != null){
                try {
                    fr.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //E:\java\javaSE\IO\src\com\pan\java1\FileReaderWriterTest.java

FileWriter写出数据

    从内存中写出数据到硬盘的文件里。
    说明:
    1. 输出操作,对应的File可以不存在的。并不会报异常
    2. File对应的硬盘中的文件如果不存在,在输出的过程中,会自动创建此文件。
       File对应的硬盘中的文件如果存在:
                如果流使用的构造器是:FileWriter(file,false) / FileWriter(file):对原有文件的覆盖
                如果流使用的构造器是:FileWriter(file,true):不会对原有文件覆盖,而是在原有文件基础上追加内容
@Test
    public void testFileWriter(){
        FileWriter fw = null;
        try {
            File file = new File("hello1.txt");
            fw = new FileWriter(file, false);//第二个参数表示覆盖还是追加
            fw.write("i have a dream\n");
            fw.write("you need have a dream1");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fw != null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
//读进内存再写出去.将hello.txt的内容复制到hello1.txt中
    @Test
    public void testFileReaderFileWriter() {
        FileReader fr = null;
        FileWriter fw = null;
        try {
            File file = new File("hello.txt");
            File file1 = new File("hello1.txt");

            fr = new FileReader(file);
            fw = new FileWriter(file1);

            char[] cbuf = new char[5];
            int len;//记录每次读入到cbuf数组中的字符的个数
            while((len = fr.read(cbuf)) != -1){
                //每次写出len个字符
                fw.write(cbuf,0,len);//0和len是索引

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr != null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fw != null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

字节流

使用FileInputStream和FileOutputStream读写文本文件

//使用字节流FileInputStream处理文本文件,可能出现乱码。英文不出乱码,英文用一个字节存的,中文要乱码,中文用三个字节存的
    @Test
    public void testFileInputStream() {
        FileInputStream fis = null;
        try {
            //1. 造文件
            File file = new File("hello.txt");

            //2.造流
            fis = new FileInputStream(file);

            //3.读数据
            byte[] buffer = new byte[5];
            int len;//记录每次读取的字节的个数
            while((len = fis.read(buffer)) != -1){

                String str = new String(buffer,0,len);
                System.out.print(str);

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis != null){
                //4.关闭资源
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

    }
    //输出:helloworld123��国���

使用FileInputStream和FileOutputStream读写非文本文件

//实现对图片的复制操作,将go.jpg复制到kun.jpg
@Test
    public void testFileInputOutputStream() throws IOException {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File src = new File("go.jpg");
            File des = new File("kun.jpg");
            fis = new FileInputStream(src);
            fos = new FileOutputStream(des);
            //复制的过程
            byte[] bytes = new byte[5];
            int len;
            while((len = fis.read(bytes)) != -1){
                fos.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
指定路径下文件的复制:
    public void copyFile(String srcPath,String destPath){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //
            File srcFile = new File(srcPath);
            File destFile = new File(destPath);

            //
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);

            //复制的过程
            byte[] buffer = new byte[1024];
            int len;
            while((len = fis.read(buffer)) != -1){
                fos.write(buffer,0,len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fos != null){
                //
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //把后一个视频替换为前一个视频
    @Test
    public void testCopyFile(){
        long start = System.currentTimeMillis();
        String srcPath = "E:\\迅雷下载\\test.mp4";
        String destPath = "E:\\迅雷下载\\hahaha\\cat.mp4";
//        String srcPath = "hello.txt";
//        String destPath = "hello3.txt";
        copyFile(srcPath,destPath);
       long end = System.currentTimeMillis();
        System.out.println("复制操作花费的时间为:" + (end - start));//618
    }

缓冲流

缓冲流(字节型)实现非文本文件的复制

/*
    使用BufferedReader和BufferedWriter实现文本文件的复制
     */
     @Test
     public void testBufferedReaderBufferedWriter(){
         BufferedReader br = null;
         BufferedWriter bw = null;
         try {
             //创建文件和相应的流
             br = new BufferedReader(new FileReader(new File("test.txt")));
             bw = new BufferedWriter(new FileWriter(new File("test1.txt")));

             //读写操作
             //方式一:使用char[]数组
//            char[] cbuf = new char[1024];
//            int len;
//            while((len = br.read(cbuf)) != -1){
//                bw.write(cbuf,0,len);
//    //            bw.flush();
//            }

             //方式二:使用String
             String data;
             while((data = br.readLine()) != null){
                 //方法一:
//                bw.write(data + "\n");//data中不包含换行符
                 //方法二:
                 bw.write(data);//data中不包含换行符
                 bw.newLine();//提供换行的操作
             }
         } catch (IOException e) {
             e.printStackTrace();
         } finally {
             //关闭资源
             if(bw != null){
                 try {
                     bw.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
             if(br != null){
                 try {
                     br.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
         }
     }

练习

//图片加密
    @Test
    public void test1() {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(new File("kun.jpg"));
            fos = new FileOutputStream(new File("kun_jiami.jpg"));
            byte[] bytes = new byte[20];
            int len;
            while((len = fis.read(bytes)) != -1){
                for (int i = 0; i < len; i++) {
                    bytes[i] = (byte) (bytes[i] ^ 5);
                }
                fos.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //图片解密
    @Test
    public void test2(){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(new File("kun_jiami.jpg"));
            fos = new FileOutputStream(new File("kun_jiemi.jpg"));
            byte[] bytes = new byte[20];
            int len;
            while((len = fis.read(bytes)) != -1){
                for (int i = 0; i < len; i++) {
                    bytes[i] = (byte) (bytes[i] ^ 5);
                }
                fos.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //E:\java\javaSE\IO\src\com\pan\exer\PicTest.java
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pk5515

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值