写在前面

今天继续讲JavaIO流的知识!

Java 文件与流操作

File 类

File 类用于表示文件和文件夹的抽象。

构造方法

  • public File(String pathname): 使用路径名创建 File 对象。
  • public File(String parent, String child): 使用父目录和子目录名称创建 File 对象。
  • public File(File parent, String child): 使用父 File 对象和子目录名称创建 File 对象。

成员方法

创建功能

  • public boolean createNewFile(): 创建一个新文件。
  • public boolean mkdir(): 创建单个目录。
  • public boolean mkdirs(): 创建目录,包括必要但不存在的父目录。

删除功能

  • public boolean delete(): 删除文件或目录。

重命名功能

  • public boolean renameTo(File dest): 重命名文件或目录。

判断功能

  • public boolean isDirectory(): 判断是否为目录。
  • public boolean isFile(): 判断是否为文件。
  • public boolean exists(): 判断文件或目录是否存在。
  • public boolean canRead(): 判断是否可读。
  • public boolean canWrite(): 判断是否可写。
  • public boolean isHidden(): 判断是否为隐藏文件。

基本获取功能

  • public String getAbsolutePath(): 获取绝对路径。
  • public String getPath(): 获取路径。
  • public String getName(): 获取文件名。
  • public long length(): 获取文件长度。
  • public long lastModified(): 获取最后修改时间。

高级获取功能

  • public String[] list(): 获取目录中的文件和目录名称。
  • public File[] listFiles(): 获取目录中的 File 对象数组。

文件名称过滤器

  • public String[] list(FilenameFilter filter): 使用文件名过滤器获取文件和目录名称。
  • public File[] listFiles(FilenameFilter filter): 使用文件名过滤器获取 File 对象数组。

字节流

字节输入流

  • 抽象父类: InputStream
    • 实现子类:
      • FileInputStream
        • 创建对象的方式:
          FileInputStream fis = new FileInputStream("路径");
          
        • 读取数据的方式:
          • 一次读取一个字节:
            int i = 0;
            while ((i = fis.read()) != -1) {
                System.out.print((char) i);
            }
            
          • 一次读取一个字节数组:
            byte[] bytes = new byte[1024];
            int length = 0;
            while ((length = fis.read(bytes)) != -1) {
                String s = new String(bytes, 0, length);
                System.out.print(s);
            }
            
          • 一次读取一个字节数组的一部分。
            byte[] bytes = new byte[1024];
            int length = 0;
            while ((length = fis.read(bytes)) != -1) {
                String s = new String(bytes, 这里写从哪个索引开始截取, 这里写自己想要截取的长度);
                System.out.print(s);
            }
            
      • BufferedInputStream
        • 创建对象的方式:
          BufferedInputStream bis = new BufferedInputStream(new FileInputStream("路径"));
          
        • 读取数据的方式:
          • 一次读取一个字节:
            int i = 0;
            while ((i = bis.read()) != -1) {
                System.out.print((char) i);
            }
            
          • 一次读取一个字节数组:
            byte[] bytes = new byte[1024];
            int length = 0;
            while ((length = bis.read(bytes)) != -1) {
                String s = new String(bytes, 0, length);
                System.out.print(s);
            }
            
          • 一次读取一个字节数组的一部分。
            byte[] bytes = new byte[1024];
            int length = 0;
            while ((length = bis.read(bytes)) != -1) {
                String s = new String(bytes, 这里写从哪个索引开始截取, 这里写自己想要截取的长度);
                System.out.print(s);
            }
            

字节输出流

  • 抽象父类: OutputStream
    • 实现子类:
      • FileOutputStream

        • 创建对象的方式:
        #创建文件输出流以写入由指定的 File对象表示的文件。
        FileOutputStream(File file) 
        
        
        #创建文件输出流以写入由指定的 File对象表示的文件。  
        FileOutputStream(File file, boolean append) 
        
        
        #创建文件输出流以指定的名称写入文件。  
        FileOutputStream(String name) 
        
        
        #创建文件输出流以指定的名称写入文件。 
        FileOutputStream(String name, boolean append) 
        
        • 写数据的方式:
          • 写一个字节:
            fos.write(i);
            
          • 写一个字节数组:
            byte[] bytes = {97, 98, 99};
            fos.write(bytes);
            
          • 写字节数组的一部分:
            byte[] bytes = {97, 98, 99};
            fos.write(bytes, 1, 2);
            
      • BufferedOutputStream

        • 创建对象的方式:
          BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("路径"));
          
        • 写数据的方式(注:这种方式写文件在写完之后需要flush一下,不然不会成功写入,后面说的以file对象进行传入参数的输入流基本上都要写完刷一下才有内容写入):
          • 写一个字节:
            bos.write(i);
            
          • 写一个字节数组:
            byte[] bytes = {97, 98, 99};
            bos.write(bytes);
            
          • 写字节数组的一部分:
            byte[] bytes = {97, 98, 99};
            bos.write(bytes, 1, 2);
            

字符流

字符流是基于字节流的,并加上了编码表的支持。

字符输入流

  • 抽象父类: Reader
    • 实现子类:
      • InputStreamReader
        • 创建对象的方式:
          InputStreamReader isr = new InputStreamReader(new FileInputStream("路径"));
          
        • 读取数据的方式:
          • 一次读取一个字符:
            int i = 0;
            while ((i = isr.read()) != -1) {
                System.out.print((char) i);
            }
            
          • 一次读取一个字符数组:
            char[] chars = new char[1024];
            int length = 0;
            while ((length = isr.read(chars)) != -1) {
                String s = new String(chars, 0, length);
                System.out.print(s);
            }
            
          • 一次读取一个字符数组的一部分。
            char[] chars = new char[1024];
            int length = 0;
            while ((length = isr.read(chars)) != -1) {
                String s = new String(chars, 开始索引, 要读的具体长度);
                System.out.print(s);
            }
            
      • FileReader
        • 创建对象的方式:
          FileReader fr = new FileReader("路径");
          
        • 读取数据的方式:
          • 一次读取一个字符:
            int i = 0;
            while ((i = fr.read()) != -1) {
                System.out.print((char) i);
            }
            
          • 一次读取一个字符数组:
            char[] chars = new char[1024];
            int length = 0;
            while ((length = fr.read(chars)) != -1) {
                String s = new String(chars, 0, length);
                System.out.print(s);
            }
            
          • 一次读取一个字符数组的一部分。
            char[] chars = new char[1024];
            int length = 0;
            while ((length = fr.read(chars)) != -1) {
                String s = new String(chars, 开始索引, 要读的具体长度);
                System.out.print(s);
            }
            
      • BufferedReader
        • 创建对象的方式:
          • 方式1:
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("路径")));
            
          • 方式2(简化版):
            BufferedReader br = new BufferedReader(new FileReader("路径"));
            
        • 读取数据的方式:
          • 一次读取一个字符:
            int i = 0;
            while ((i = br.read()) != -1) {
                System.out.print((char) i);
            }
            
          • 一次读取一个字符数组:
            char[] chars = new char[1024];
            int length = 0;
            while ((length = br.read(chars)) != -1) {
                String s = new String(chars, 0, length);
                System.out.print(s);
            }
            
          • 一次读取一个字符数组的一部分。
            char[] chars = new char[1024];
            int length = 0;
            while ((length = br.read(chars)) != -1) {
                String s = new String(chars, 开始索引, 具体长度);
                System.out.print(s);
            }
            
          • 一次读取一行数据(不包括换行符):
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.print(line);
            }
            

字符输出流

  • 抽象父类: Writer
    • 实现子类:
      • OutputStreamWriter

        • 创建对象的方式:
          OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("路径"));
          
        • 写数据的方式:
        • 一次写一个字符。
          osw.write(i);
          
        • 一次写一个字符数组。
          char[] chars = {'a', 'b', 'c'};
          osw.write(chars);
          
        • 一次写一个字符数组的一部分。
          char[] chars = {'a', 'b', 'c'};
          osw.write(chars, 1, 2);
          
        • 一次写一个字符串。
          osw.write("example");
          
        • 一次写一个字符串的一部分。
          osw.write("example", 1, 4);
          
      • FileWriter

        • 创建对象的方式:
          FileWriter fw = new FileWriter("路径");
          
        • 写数据的方式:
          • 一次写一个字符。
            fw.write(i);
            
          • 一次写一个字符数组。
            char[] chars = {'a', 'b', 'c'};
            fw.write(chars);
            
          • 一次写一个字符数组的一部分。
            char[] chars = {'a', 'b', 'c'};
            fw.write(chars, 1, 2);
            
          • 一次写一个字符串。
            fw.write("example");
            
          • 一次写一个字符串的一部分。
            fw.write("example", 1, 4);
            
      • BufferedWriter

        • 创建对象的方式:
          • 方式1:
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("路径")));
            
          • 方式2(简化版):
            BufferedWriter bw = new BufferedWriter(new FileWriter("路径"));
            
        • 写数据的方式:
          • 一次写一个字符:
            bw.write(i);
            
          • 一次写一个字符数组:
            char[] chars = {'a', 'b', 'c'};
            bw.write(chars);
            
          • 一次写一个字符数组的一部分:
            char[] chars = {'a', 'b', 'c'};
            bw.write(chars, 1, 2);
            
          • 一次写一个字符串:
            bw.write("example");
            
          • 一次写一个字符串的一部分:
            bw.write("example", 1, 4);
            
          • 特殊功能:自动生成换行符:
            bw.newLine();
            

文件的读写复制

字节流

  • 普通字节输入输出流:一次读写一个字节。
    ```java

        ```
    
  • 带缓冲的字节输入输出流:一次读写一个字节。
    ```java

        ```
    
  • 普通字节输入输出流:一次读写一个字节数组。
    ```java

        ```
    
  • 带缓冲的字节输入输出流:一次读写一个字节数组(此方式的效率最高)。
    ```java

        ```
    

字符流

  • 普通字符输入输出流:一次读写一个字符。
  • 普通字符输入输出流:一次读写一个字符数组。
  • 简化写法字符输入输出流:一次读写一个字符。
  • 简化写法字符输入输出流:一次读写一个字符数组。
  • 字符缓冲输入输出流:一次读写一个字符。
  • 字符缓冲输入输出流:一次读写一个字符数组。
  • 字符缓冲输入输出流:一次读写一行数据。