IO流、File类、节点流、缓冲流、转换流

导读:本篇文章讲解 IO流、File类、节点流、缓冲流、转换流,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

目录

IO流

流的分类:

流的体系结构 

抽象基类、字节流和缓冲流的区别

输入、输出的标准化过程

File类

File类的理解

File类的实例化

节点流(文件流)

FileReader/FileWriter的使用:

FileInputStream / FileOutStream的使用:

缓冲流

缓冲流涉及到的类

缓冲流的作用 

 典型代码

转换流

转换流涉及到的类

作用:提供字节流与字符流之间的转换

 ​

 InputStreamReader的使用,实现字节的输入流到字符的输入流的转换


IO流

  • 流的分类:

①操作数据单位:字节流、字符流;

②数据的流向:输入流、输出流;

③流的角色:节点流、处理流。

IO流、File类、节点流、缓冲流、转换流

  • 流的体系结构 

IO流、File类、节点流、缓冲流、转换流

 红框对应的是IO流中的四个抽象基类。

  • 抽象基类、字节流和缓冲流的区别

IO流、File类、节点流、缓冲流、转换流

  • 输入、输出的标准化过程

  • 输入过程

① 创建File类的对象,指明读取的数据的来源。(要求此文件一定要存在)
② 创建相应的输入流,将File类的对象作为参数,传入流的构造器中
③ 具体的读入过程:
    创建相应的byte[] 或 char[]。
④ 关闭流资源
说明:程序中出现的异常需要使用try-catch-finally处理。

  • 输出过程

① 创建File类的对象,指明写出的数据的位置。(不要求此文件一定要存在)
② 创建相应的输出流,将File类的对象作为参数,传入流的构造器中
③ 具体的写出过程:
    write(char[]/byte[] buffer,0,len)
④ 关闭流资源
说明:程序中出现的异常需要使用try-catch-finally处理。
 

File类

  • File类的理解

        1.File类的一个对象,代表一个文件或一个文件目录(俗称文件夹)

        2.File类声明在java.io包中

        3.File类中涉及到关于文件或文件目录的创建、删除、重命名、修改时间、文件大小等方法;并未涉及到写入或读取文件内容的操作。如果需要读取或写入文件内容,必须使用IO流来完成。

        4.后续File类的对象会作为参数传递到流的构造器中,指明读取或写入的“终点”。

  • File类的实例化

        1.常用构造器

File(String filePath)

File(String filePath, String childPath)

File(File filePath,String childPath)

        2.路径的分类

        相对路径:相较于某个路径下,指明的路径。

        绝对路径:包含盘符在内的文件或文件目录的路径。

IDEA中:
如果大家开发使用JUnit中的单元测试方法测试,相对路径即为当前Module下。
如果大家使用main()测试,相对路径即为当前的Project下。
Eclipse中:
不管使用单元测试方法还是使用main()测试,相对路径都是当前的Project下。

        3.File类的常用方法

IO流、File类、节点流、缓冲流、转换流

 IO流、File类、节点流、缓冲流、转换流

IO流、File类、节点流、缓冲流、转换流

节点流(文件流)

  • FileReader/FileWriter的使用:

        1. FileReader的使用

        2.FileWriter的使用

        3.文本文件的复制(不能使用字符流来处理图片等字节数据)

  • FileInputStream / FileOutStream的使用:

    //使用字节流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();//try-catch快捷键是Alt+Enter
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

    }

 hello.txt中的数据为helloworld中国,但是最后在控制台的输出为

helloworld中��

//1. 对于文本文件(.txt,.java,.c,.cpp),使用字符流处理
//2. 对于非文本文件(.jpg,.mp3,.mp4,.avi,.doc,.ppt,...),使用字节流处理
/*
实现对图片的复制操作
 */
@Test
public void testFileInputOutputStream()  {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        //1.造文件
        File srcFile = new File("爱情与友情.jpg");
        File destFile = new File("爱情与友情2.jpg");

        //2.造流
        fis = new FileInputStream(srcFile);
        fos = new FileOutputStream(destFile);

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

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(fos != null){
            //4.关闭流
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(fis != null){
            try {
                fis.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 = "C:\\Users\\Administrator\\Desktop\\01-视频.avi";
//        String destPath = "C:\\Users\\Administrator\\Desktop\\02-视频.avi";


        String srcPath = "hello.txt";
        String destPath = "hello3.txt";

        copyFile(srcPath,destPath);


        long end = System.currentTimeMillis();

        System.out.println("复制操作花费的时间为:" + (end - start)+"毫秒");//4毫秒

    }

}

缓冲流

  • 缓冲流涉及到的类

BufferedInputStream
BufferedOutputStream
BufferedReader
BufferedWriter

  • 缓冲流的作用 

        作用:提供流的读取、写入的速度。

        提高读写速度的原因:内部提供了一个缓冲区。默认情况下是8kb.

IO流、File类、节点流、缓冲流、转换流

  •  典型代码

        1.使用BufferedInputStream和BufferedOutputStream:处理非文本文件

//实现文件复制的方法
    public void copyFileWithBuffered(String srcPath,String destPath){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            //1.造文件
            File srcFile = new File(srcPath);
            File destFile = new File(destPath);
            //2.造流
            //2.1 造节点流
            FileInputStream fis = new FileInputStream((srcFile));
            FileOutputStream fos = new FileOutputStream(destFile);
            //2.2 造缓冲流
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);

            //BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("a.jpg")))

            //BufferedOutputStream bis = new BufferedOutputStream(new FileOutputStream(new File("b.jpg")))


            //3.复制的细节:读取、写入
            byte[] buffer = new byte[1024];
            int len;
            while((len = bis.read(buffer)) != -1){
                bos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.资源关闭
            //要求:先关闭外层的流,再关闭内层的流
            if(bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

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

            }
            //说明:关闭外层流的同时,内层流也会自动的进行关闭。关于内层流的关闭,我们可以省略.
//        fos.close();
//        fis.close();
        }
    }

        2.使用BufferedReader和BufferedWriter:处理文本文件

    @Test
    public void testBufferedReaderBufferedWriter(){
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            //创建文件和相应的流
            br = new BufferedReader(new FileReader(new File("dbcp.txt")));
            bw = new BufferedWriter(new FileWriter(new File("dbcp1.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();
                }

            }
        }

    }

}

转换流

  • 转换流涉及到的类

InputStreamReader:将一个字节的输入流转换为字符的输入流
解码:字节、字节数组  —>字符数组、字符串

OutputStreamWriter:将一个字符的输出流转换为字节的输出流
编码:字符数组、字符串 —> 字节、字节数组

说明:编码决定了解码的方式

  • 作用:提供字节流与字符流之间的转换

 IO流、File类、节点流、缓冲流、转换流

  •  InputStreamReader的使用,实现字节的输入流到字符的输入流的转换

    @Test
    public void test1() throws IOException {

        FileInputStream fis = new FileInputStream("dbcp.txt");//字节流处理的文件
//        InputStreamReader isr = new InputStreamReader(fis);//使用系统默认的字符集
        //参数2指明了字符集,具体使用哪个字符集,取决于文件dbcp.txt保存时使用的字符集
        InputStreamReader isr = new InputStreamReader(fis,"UTF-8");//使用系统默认的字符集

        char[] cbuf = new char[20];
        int len;
        while((len = isr.read(cbuf)) != -1){
            String str = new String(cbuf,0,len);
            System.out.print(str);
        }

        isr.close();

    }

之后再将一个文件的字符格式变成另外一个文件的字符格式

    @Test
    public void test2() throws Exception {
        //1.造文件、造流
        File file1 = new File("dbcp.txt");
        File file2 = new File("dbcp_gbk.txt");

        FileInputStream fis = new FileInputStream(file1);
        FileOutputStream fos = new FileOutputStream(file2);

        InputStreamReader isr = new InputStreamReader(fis,"utf-8");
        OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");

        //2.读写过程
        char[] cbuf = new char[20];
        int len;
        while((len = isr.read(cbuf)) != -1){
            osw.write(cbuf,0,len);
        }

        //3.关闭资源
        isr.close();
        osw.close();


    }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/83293.html

(0)
小半的头像小半

相关推荐

半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!