JavaSE基础知识分享(十)
  YQSzfXzmOgFb 29天前 43 0

写在前面

今天继续讲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();
            

文件的读写复制

字节流

  • 普通字节输入输出流:一次读写一个字节。
    FileInputStream inputPath = new FileInputStream("inputpath");
    FileOutputStream outputPath = new FileOutputStream("outpath");

    int i = 0;
    while ((i = inputPath.read()) != -1) {
        outputPath.write((char) i);
     }
    inputPath.close();
    outputPath.close();
  • 带缓冲的字节输入输出流:一次读写一个字节。
    BufferedInputStream inputpath = new BufferedInputStream(new FileInputStream("inputpath"));
    BufferedOutputStream outputpath = new BufferedOutputStream(new FileOutputStream("outpath"));

    int i = 0;

    while ((i = inputpath.read()) != -1) {
        outputpath.write((char) i);
        outputpath.flush;
     }

    inputpath.close();
    outputpath.close();
        
  • 普通字节输入输出流:一次读写一个字节数组。
    FileInputStream inputPath = new FileInputStream("inputpath");
    FileOutputStream outputPath = new FileOutputStream("outpath");

    byte[] bytes = new byte[1024];
    int length = 0;
    while ((length = inputPath.read(bytes)) != -1) {
        outputPath.write(bytes, 0, length);
        outputPath.flush();
     }
    inputPath.close();
    outputPath.close();
  • 带缓冲的字节输入输出流:一次读写一个字节数组(此方式的效率最高)。
    BufferedInputStream inputpath = new BufferedInputStream(new FileInputStream("inputpath"));
    BufferedOutputStream outputpath = new BufferedOutputStream(new FileOutputStream("outpath"));

    byte[] bytes = new byte[1024];
    int length = 0;
    while ((length = inputpath.read(bytes)) != -1) {
        outputpath.write(bytes, 0, length);
        outputpath.flush();
    }

    inputpath.close();
    outputpath.close();

字符流

  • 普通字符输入输出流:一次读写一个字符。
    InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("inputpath"));
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("outputpath"));

    int i = 0;
    while ((i = inputStreamReader.read()) != -1) {
        outputStreamWriter.write(i);
        outputStreamWriter.flush();
    }

    inputStreamReader.close();
    outputStreamWriter.close();
  • 普通字符输入输出流:一次读写一个字符数组。
    InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("inputpath"));
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("outputpath"));

    char[] chars = new char[1024];
    int length = 0;
    while ((length = inputStreamReader.read(chars)) != -1) {
        outputStreamWriter.write(chars,0,length);
        outputStreamWriter.flush();
    }

    inputStreamReader.close();
    outputStreamWriter.close();
  • 简化写法字符输入输出流:一次读写一个字符。
    FileReader fileReader = new FileReader("inputpath");
    FileWriter fileWriter = new FileWriter("outputpath");

    int i = 0;
    while ((i = fileReader.read()) != -1) {
        fileWriter.write(i);
        fileWriter.flush();
    }

    fileWriter.close();
    fileReader.close();
  • 简化写法字符输入输出流:一次读写一个字符数组。
    FileReader fileReader = new FileReader("inputpath");
    FileWriter fileWriter = new FileWriter("src/com/shujia/data/b.txt");

    char[] chars = new char[1024];
    int length = 0;
    while ((length = fileReader.read(chars)) != -1) {
        fileWriter.write(chars,0,length);
        fileWriter.flush();
    }

    fileWriter.close();
    fileReader.close();
  • 字符缓冲输入输出流:一次读写一个字符。
    BufferedReader bufferedReader = new BufferedReader(new FileReader("inputpath"));
    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("outpath"));

    int i = 0;
    while ((i = bufferedReader.read()) != -1) {
         bufferedWriter.write(i);
         bufferedWriter.flush();
    }

    bufferedWriter.close();
    bufferedReader.close();
  • 字符缓冲输入输出流:一次读写一个字符数组。
    BufferedReader bufferedReader = new BufferedReader(new FileReader("inputpath"));
    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("outpath"));

    char[] chars = new char[1024];
    int length = 0;
    while ((length = bufferedReader.read(chars)) != -1) {
    bufferedWriter.write(chars,0,length);
    bufferedWriter.flush();
    }

    bufferedWriter.close();
    bufferedReader.close();
  • 字符缓冲输入输出流:一次读写一行数据。
    BufferedReader bufferedReader = new BufferedReader(new FileReader("inputpath"));
    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("outpath"));

    String i = null;
    while ((i = bufferedReader.readLine()) != null) {
        bufferedWriter.write(i);
        bufferedWriter.newLine();
        bufferedWriter.flush();
     }

    bufferedWriter.close();
    bufferedReader.close();
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 29天前 0

暂无评论

推荐阅读
  FHUfYd9S4EP5   2天前   19   0   0 Java
  sSh3rBaiME5f   3天前   22   0   0 Java
  qCe06rFCa8NK   2天前   15   0   0 Java
  ZTo294hNoDcA   2天前   19   0   0 Java
  FHUfYd9S4EP5   2天前   16   0   0 Java
  QGiRIwDaZAe8   3天前   18   0   0 Java
YQSzfXzmOgFb