• File类和IO流


    File类和IO流

    IO流是什么?

    • 可以将数据从本地文件中读取出来
    • 可以将数据从内存保存到本地文件

    File类是什么?

    • 在读写数据时告诉虚拟机要操作的(文件文件夹)在哪
    • 对(文件文件夹)本身进行操作。包括创建,删除等。

    File类

    File:它是文件和目录路径名的抽象表示

    • 文件和目录可以通过File封装成对象
    • File封装的对象仅仅是一个路径名。它可以是存在的,也可以是不存在的。
    File(String pathname)
        通过将给定的路径名字符串转换为抽象路径名来创建新的File实例
            File() file = new File("C:\\test\\a.txt");
    File(String parent,String child)
        从父路径名字符串和子路径名字符串创健新的File实例
            // 路径的字符串拼接
    File(File parent,String child)
        从父抽象路径名和子路径名字符串创建新的File实例
            // 路径的文件和字符串拼接
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    创建功能方法

    createNewFile()

    创建一个空的文件夹

    1. 如果文件已经存在,那么就会返回false
    2. 那么反之
    3. 不管方法调用者有没有写后缀名,只能创建文件
    File() file = new File("D:\\test.txt");
    file.createNewFile();
    
    • 1
    • 2
    mkdir()

    创建一个空的单级文件夹

    1. 只能创建单级文件夹,不可以创建多级文件夹
    File() file = new File("D:\\test");
    file.mkdir();
    
    • 1
    • 2
    mkdirs()

    创建一个多级文件夹

    1. 可以创建单级文件夹,也可以创建多级文件夹
    File() file = new File("D:\\a\\b\\c");
    file.mkdirs();
    
    • 1
    • 2

    删除功能

    delete()

    删除文件或者空文件夹

    File() file = new File("D:\\test.txt");
    file.delete();
    
    • 1
    • 2

    注意:

    1. 不走回收站
    2. 如果删除的是文件,直接删除,如果是文件夹,只能删除空的文件夹

    获取和判断

    public boolean isDirectory()
        测试此抽象路径名表示的File是否为目录(文件夹)
            file.isDirectory();
    public boolean isFile()
        测试此抽象路径名表示的File是否为文件
    public boolean exists()
        测试此抽象路径名表示的File是否存在
        // 判断当前项目下文件是否存在
    public String getName()
        返回由此抽象路径名表示的文件或目录的名称
        // 文件返回的是文件的名字和后缀
        // 文件夹返回的是文件名
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    高级获取
    public String[] list()
        返回此抽象路径名表示的目录中的文件名称
    public File[] listFiles()
        返回此抽象路径名表示的目录中的文件和目录的File对象数组
    
    • 1
    • 2
    • 3
    • 4
    File() file = new File("D:");
    file.listFiles();
    // 返回的是一个File数组
    
    • 1
    • 2
    • 3

    注意:

    1. 路径不存在时,返回的是null
    2. 调用者是文件时,返回的是null
    3. 调用者是空文件夹时,返回的是长度为0的数组
    4. 调用者中有隐藏文件夹时,也会一并返回
    5. 调用者是需要权限才可以进入时,返回null

    IO

    字节流

    读取FileInputStream
    // 文件不存在就会出错
    FileInputStream fis = new FileInputStream("D:\\a.txt");
    // 返回的是对应的码表的数值
    int fl = fil.read();
    // 释放资源
    fis.close();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    上面的是读取单字节,要读取多字节就是用循环,读取到最后时,不存在的会返回-1,作为循环条件

    // 读取多个数据
    // 文件不存在就会出错
            FileInputStream fis = null;
            try {
                fis = new FileInputStream("D:\\文档\\java\\File和IO.md");
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }
    
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream("D:\\文档\\java\\File和IO1.md");
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }
    // 创建一个数组,一次收集多少字节
            byte[] bt = new byte[1024];
            int len;
    
            while ((len = fis.read(bt)) != -1) {
                // 可以进行写的操作
                fos.write(bt, 0, len);
            }
    // 释放资源
            fis.close();
            fos.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
    try (FileInputStream fis = new FileInputStream("D:\\文档\\java\\File和IO.txt"); FileOutputStream fos = new FileOutputStream("D:\\文档												\\java\\File和IO1.md")) 
    {
        // 创建一个数组
        byte[] bt = new byte[1024];
        int len;
        while ((len = fis.read(bt)) != -1) {
            // 可以进行写的操作
            fos.write(bt, 0, len);
        }
    } catch (FileNotFoundException e) {
        System.out.println(e.toString());
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    写数据FileOutputStream
    // 文件存在就会清空里面的内容再进行写操作,需要不清空写入,那就更改第二个参数变为true
    // 文件不存在就创建一个文件
    FileOutputStream fos = new FileOutputStream(name:"D:\\a.txt"); // 清空
    FileOutputStream fos = new FileOutputStream(name:"D:\\a.txt", true); // 不清空
    // 整数写进的是码表对应的字母
    fos.write(97);
    // 释放资源
    fos.close();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    void write(int b)
        一次写一个字节数据
    void write(byte[] b)
        一次写一个字节数组数据
            byte[] by = {97,98,99};
            fos.write(by);
    void write(byte[] b,int off,int len)
        一次写一个字节数组的部分数据
            fos.write(by, 0, 2);
            // 从0索引开始,一共写进两个
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    换行

    \r\n

    不能直接写进,使用方法转换为byte数组

     fos.write("\r\n".getBytes());
    
    • 1
    字节缓冲流

    只是为了提高效率, 真正工作的还是字节流

    //创建一个字节缓冲输入流
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\文档\\java\\File和IO.md"));
    //创建一个字节缓冲输出流
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\文档\\java\\File和IO.txt"));
    byte[] bytes = new byte[1024];
    int len;
    while ((len = bis.read(bytes)) != -1) {
        bos.write(bytes, 0, len);
    };
    bis.close();
    bos.close();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\文档\\java\\File和IO.md")); BufferedOutputStream bos = new 													BufferedOutputStream(new FileOutputStream("D:\\文档\\java\\File和IO.txt"));) 
    {
        byte[] bytes = new byte[1024];
        int len;
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
        };
        bis.close();
        bos.close();
    } catch (FileNotFoundException e) {
        System.out.println(e.toString());
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    字符流

    windows默认使用码表为:GBK,一个字符两个字节。
    idea和以后工作默认使用UnicodeUTF-8编解码格式,一个中文三个字节。

    字符流读取中文的过程

    字符流 = 字节流 + 编码表

    基础知识:

    不管是在哪张码表中,中文的第一个字节一定是负数。

    编码
    String s = "多喝热水";
    byte[] bt = s.getBytes();
    System.out.println(Arrays.toString(bt));// [-27, -92, -102, -27, -106, -99, -25, -125, -83, -26, -80, -76]
    
    • 1
    • 2
    • 3
    解码
    String st = new String(bt);// 默认是Unicode,可以指定码表,但是一定要和编码的码表一致,不然会出现乱码String(bt, "gbk")
    System.out.println(st);// 多喝热水
    
    • 1
    • 2
    写数据
    void write (int c)
        写一个字符
    void write (char[] cbuf)
        写出一个字符数组
    void write()(char[] cbuf,int off,int len)
        写出字符数组的一部分
    void write (String str)
        写一个字符串
    void write (String str,int off,int len)
        写一个字符串的一部分
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    // 文件不存在可以自动创建,但是上级文件夹是必须要确保存在,是无法自动创建文件夹的,会出错
    // 清空之后再进行写进数据
    FileWriter fw = new FileWriter("D:\\");
    fw.write("hello");
    // 刷新文件,可以继续写进数据
    fw.flush();
    //释放资源,释放之前会刷新一遍文件,不可以继续写进数据
    fw.close;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    读数据
    FileReader fr = new FileReader("D:\\a.txt");
    char[] ch = new char[1024];
    int len;
    while((len = fr.read(ch)) != -1){
        String s = new String(ch, 0, len);
    }
    fr.close();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    字符缓冲流
    BufferedReader br = new BufferedReader(new FileReader("D:\\a.txt"));
    BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\a.txt"));
    
    • 1
    • 2
    特有的方法

    readLine()

    一次读取一行

    // 读取的行内没有内容返回的是null
    BufferedReader br = new BufferedReader(new FileReader("D:\\a.txt"));
    String s = br.readLine();
    br.close();
    
    • 1
    • 2
    • 3
    • 4

    newLine()

    换行

    BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\javaProject\\User.txt"));
    bw.write("多喝热水");
    bw.newLine();
    bw.write("多喝热水");
    bw.close();
    
    • 1
    • 2
    • 3
    • 4
    • 5

    字节流转化字符流

    try (Socket socket = new Socket("127.0.0.1",5500)) {
      // 得到的字节流转化为字符流
      BufferedReader bufferedReaders = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    }
    ---
    BufferedWriter bufferedReaders = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    // 刷新缓冲区
    bufferedReaders.flush();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    仅供参考

  • 相关阅读:
    janus streaming运行说明
    hadoop1.2.1伪分布式搭建
    Android问题笔记 - NoSuchmethodException: could not find Fragment constructor
    linux驱动注册注销中断处理函数
    基于hadoop的气象数据可视化分析
    智慧公厕领先品牌:卫生、智能、环保
    Java8IntStream数值流的常用操作以及与装箱和拆箱的关系
    难受啊,早饭忘记吃了
    66个Python练手项目,附源码
    如何提取视频中的音频转为mp3
  • 原文地址:https://blog.csdn.net/xiaoChenPengYou/article/details/126675461