JavaSE基础知识分享(十)

打印 上一主题 下一主题

主题 867|帖子 867|积分 2601

写在前面

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

        • 创建对象的方式:
          1. FileInputStream fis = new FileInputStream("路径");
          复制代码
        • 读取数据的方式:

          • 一次读取一个字节:
            1. int i = 0;
            2. while ((i = fis.read()) != -1) {
            3.     System.out.print((char) i);
            4. }
            复制代码
          • 一次读取一个字节数组:
            1. byte[] bytes = new byte[1024];
            2. int length = 0;
            3. while ((length = fis.read(bytes)) != -1) {
            4.     String s = new String(bytes, 0, length);
            5.     System.out.print(s);
            6. }
            复制代码
          • 一次读取一个字节数组的一部分。
            1. byte[] bytes = new byte[1024];
            2. int length = 0;
            3. while ((length = fis.read(bytes)) != -1) {
            4.     String s = new String(bytes, 这里写从哪个索引开始截取, 这里写自己想要截取的长度);
            5.     System.out.print(s);
            6. }
            复制代码


      • BufferedInputStream

        • 创建对象的方式:
          1. BufferedInputStream bis = new BufferedInputStream(new FileInputStream("路径"));
          复制代码
        • 读取数据的方式:

          • 一次读取一个字节:
            1. int i = 0;
            2. while ((i = bis.read()) != -1) {
            3.     System.out.print((char) i);
            4. }
            复制代码
          • 一次读取一个字节数组:
            1. byte[] bytes = new byte[1024];
            2. int length = 0;
            3. while ((length = bis.read(bytes)) != -1) {
            4.     String s = new String(bytes, 0, length);
            5.     System.out.print(s);
            6. }
            复制代码
          • 一次读取一个字节数组的一部分。
            1. byte[] bytes = new byte[1024];
            2. int length = 0;
            3. while ((length = bis.read(bytes)) != -1) {
            4.     String s = new String(bytes, 这里写从哪个索引开始截取, 这里写自己想要截取的长度);
            5.     System.out.print(s);
            6. }
            复制代码




字节输出流


  • 抽象父类: OutputStream

    • 实现子类:

      • FileOutputStream

        • 创建对象的方式:
        1. #创建文件输出流以写入由指定的 File对象表示的文件。
        2. FileOutputStream(File file)
        3. #创建文件输出流以写入由指定的 File对象表示的文件。  
        4. FileOutputStream(File file, boolean append)
        5. #创建文件输出流以指定的名称写入文件。  
        6. FileOutputStream(String name)
        7. #创建文件输出流以指定的名称写入文件。
        8. FileOutputStream(String name, boolean append)
        复制代码

        • 写数据的方式:

          • 写一个字节:
            1. fos.write(i);
            复制代码
          • 写一个字节数组:
            1. byte[] bytes = {97, 98, 99};
            2. fos.write(bytes);
            复制代码
          • 写字节数组的一部分:
            1. byte[] bytes = {97, 98, 99};
            2. fos.write(bytes, 1, 2);
            复制代码


      • BufferedOutputStream

        • 创建对象的方式:
          1. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("路径"));
          复制代码
        • 写数据的方式(注:这种方式写文件在写完之后需要flush一下,不然不会成功写入,后面说的以file对象进行传入参数的输入流基本上都要写完刷一下才有内容写入):

          • 写一个字节:
            1. bos.write(i);
            复制代码
          • 写一个字节数组:
            1. byte[] bytes = {97, 98, 99};
            2. bos.write(bytes);
            复制代码
          • 写字节数组的一部分:
            1. byte[] bytes = {97, 98, 99};
            2. bos.write(bytes, 1, 2);
            复制代码




字符流

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


  • 抽象父类: Reader

    • 实现子类:

      • InputStreamReader

        • 创建对象的方式:
          1. InputStreamReader isr = new InputStreamReader(new FileInputStream("路径"));
          复制代码
        • 读取数据的方式:

          • 一次读取一个字符:
            1. int i = 0;
            2. while ((i = isr.read()) != -1) {
            3.     System.out.print((char) i);
            4. }
            复制代码
          • 一次读取一个字符数组:
            1. char[] chars = new char[1024];
            2. int length = 0;
            3. while ((length = isr.read(chars)) != -1) {
            4.     String s = new String(chars, 0, length);
            5.     System.out.print(s);
            6. }
            复制代码
          • 一次读取一个字符数组的一部分。
            1. char[] chars = new char[1024];
            2. int length = 0;
            3. while ((length = isr.read(chars)) != -1) {
            4.     String s = new String(chars, 开始索引, 要读的具体长度);
            5.     System.out.print(s);
            6. }
            复制代码


      • FileReader

        • 创建对象的方式:
          1. FileReader fr = new FileReader("路径");
          复制代码
        • 读取数据的方式:

          • 一次读取一个字符:
            1. int i = 0;
            2. while ((i = fr.read()) != -1) {
            3.     System.out.print((char) i);
            4. }
            复制代码
          • 一次读取一个字符数组:
            1. char[] chars = new char[1024];
            2. int length = 0;
            3. while ((length = fr.read(chars)) != -1) {
            4.     String s = new String(chars, 0, length);
            5.     System.out.print(s);
            6. }
            复制代码
          • 一次读取一个字符数组的一部分。
            1. char[] chars = new char[1024];
            2. int length = 0;
            3. while ((length = fr.read(chars)) != -1) {
            4.     String s = new String(chars, 开始索引, 要读的具体长度);
            5.     System.out.print(s);
            6. }
            复制代码


      • BufferedReader

        • 创建对象的方式:

          • 方式1:
            1. BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("路径")));
            复制代码
          • 方式2(简化版):
            1. BufferedReader br = new BufferedReader(new FileReader("路径"));
            复制代码

        • 读取数据的方式:

          • 一次读取一个字符:
            1. int i = 0;
            2. while ((i = br.read()) != -1) {
            3.     System.out.print((char) i);
            4. }
            复制代码
          • 一次读取一个字符数组:
            1. char[] chars = new char[1024];
            2. int length = 0;
            3. while ((length = br.read(chars)) != -1) {
            4.     String s = new String(chars, 0, length);
            5.     System.out.print(s);
            6. }
            复制代码
          • 一次读取一个字符数组的一部分。
            1. char[] chars = new char[1024];
            2. int length = 0;
            3. while ((length = br.read(chars)) != -1) {
            4.     String s = new String(chars, 开始索引, 具体长度);
            5.     System.out.print(s);
            6. }
            复制代码
          • 一次读取一行数据(不包罗换行符):
            1. String line = null;
            2. while ((line = br.readLine()) != null) {
            3.     System.out.print(line);
            4. }
            复制代码




字符输出流


  • 抽象父类: Writer

    • 实现子类:

      • OutputStreamWriter

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

      • FileWriter

        • 创建对象的方式:
          1. FileWriter fw = new FileWriter("路径");
          复制代码
        • 写数据的方式:

          • 一次写一个字符。
            1. fw.write(i);
            复制代码
          • 一次写一个字符数组。
            1. char[] chars = {'a', 'b', 'c'};
            2. fw.write(chars);
            复制代码
          • 一次写一个字符数组的一部分。
            1. char[] chars = {'a', 'b', 'c'};
            2. fw.write(chars, 1, 2);
            复制代码
          • 一次写一个字符串。
            1. fw.write("example");
            复制代码
          • 一次写一个字符串的一部分。
            1. fw.write("example", 1, 4);
            复制代码


      • BufferedWriter

        • 创建对象的方式:

          • 方式1:
            1. BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("路径")));
            复制代码
          • 方式2(简化版):
            1. BufferedWriter bw = new BufferedWriter(new FileWriter("路径"));
            复制代码

        • 写数据的方式:

          • 一次写一个字符:
            1. bw.write(i);
            复制代码
          • 一次写一个字符数组:
            1. char[] chars = {'a', 'b', 'c'};
            2. bw.write(chars);
            复制代码
          • 一次写一个字符数组的一部分:
            1. char[] chars = {'a', 'b', 'c'};
            2. bw.write(chars, 1, 2);
            复制代码
          • 一次写一个字符串:
            1. bw.write("example");
            复制代码
          • 一次写一个字符串的一部分:
            1. bw.write("example", 1, 4);
            复制代码
          • 特殊功能:自动生成换行符:
            1. bw.newLine();
            复制代码




文件的读写复制

字节流


  • 平凡字节输入输出流:一次读写一个字节。
    1.   FileInputStream inputPath = new FileInputStream("inputpath");
    2.   FileOutputStream outputPath = new FileOutputStream("outpath");
    3.   int i = 0;
    4.   while ((i = inputPath.read()) != -1) {
    5.       outputPath.write((char) i);
    6.   }
    7.   inputPath.close();
    8.   outputPath.close();
    复制代码
  • 带缓冲的字节输入输出流:一次读写一个字节。
    1.   BufferedInputStream inputpath = new BufferedInputStream(new FileInputStream("inputpath"));
    2.   BufferedOutputStream outputpath = new BufferedOutputStream(new FileOutputStream("outpath"));
    3.   int i = 0;
    4.   while ((i = inputpath.read()) != -1) {
    5.       outputpath.write((char) i);
    6.       outputpath.flush;
    7.   }
    8.   inputpath.close();
    9.   outputpath.close();
    复制代码
  • 平凡字节输入输出流:一次读写一个字节数组。
    1.   FileInputStream inputPath = new FileInputStream("inputpath");
    2.   FileOutputStream outputPath = new FileOutputStream("outpath");
    3.   byte[] bytes = new byte[1024];
    4.   int length = 0;
    5.   while ((length = inputPath.read(bytes)) != -1) {
    6.       outputPath.write(bytes, 0, length);
    7.       outputPath.flush();
    8.   }
    9.   inputPath.close();
    10.   outputPath.close();
    复制代码
  • 带缓冲的字节输入输出流:一次读写一个字节数组(此方式的效率最高)。
    1.   BufferedInputStream inputpath = new BufferedInputStream(new FileInputStream("inputpath"));
    2.   BufferedOutputStream outputpath = new BufferedOutputStream(new FileOutputStream("outpath"));
    3.   byte[] bytes = new byte[1024];
    4.   int length = 0;
    5.   while ((length = inputpath.read(bytes)) != -1) {
    6.       outputpath.write(bytes, 0, length);
    7.       outputpath.flush();
    8.   }
    9.   inputpath.close();
    10.   outputpath.close();
    复制代码
字符流


  • 平凡字符输入输出流:一次读写一个字符。
    1.   InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("inputpath"));
    2.   OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("outputpath"));
    3.   int i = 0;
    4.   while ((i = inputStreamReader.read()) != -1) {
    5.       outputStreamWriter.write(i);
    6.       outputStreamWriter.flush();
    7.   }
    8.   inputStreamReader.close();
    9.   outputStreamWriter.close();
    复制代码
  • 平凡字符输入输出流:一次读写一个字符数组。
    1.   InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("inputpath"));
    2.   OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("outputpath"));
    3.   char[] chars = new char[1024];
    4.   int length = 0;
    5.   while ((length = inputStreamReader.read(chars)) != -1) {
    6.       outputStreamWriter.write(chars,0,length);
    7.       outputStreamWriter.flush();
    8.   }
    9.   inputStreamReader.close();
    10.   outputStreamWriter.close();
    复制代码
  • 简化写法字符输入输出流:一次读写一个字符。
    1.   FileReader fileReader = new FileReader("inputpath");
    2.   FileWriter fileWriter = new FileWriter("outputpath");
    3.   int i = 0;
    4.   while ((i = fileReader.read()) != -1) {
    5.       fileWriter.write(i);
    6.       fileWriter.flush();
    7.   }
    8.   fileWriter.close();
    9.   fileReader.close();
    复制代码
  • 简化写法字符输入输出流:一次读写一个字符数组。
    FileReader fileReader = new FileReader("src/com/shujia/data/a.txt");
    FileWriter fileWriter = new FileWriter("src/com/shujia/data/b.txt");
    1.   char[] chars = new char[1024];
    2.   int length = 0;
    3.   while ((length = fileReader.read(chars)) != -1) {
    4.       fileWriter.write(chars,0,length);
    5.       fileWriter.flush();
    6.   }
    7.   fileWriter.close();
    8.   fileReader.close();
    复制代码
  • 字符缓冲输入输出流:一次读写一个字符。
    1.   BufferedReader bufferedReader = new BufferedReader(new FileReader("src/com/shujia/data/a.txt"));
    2.   BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("src/com/shujia/data/b.txt"));
    3.   int i = 0;
    4.   while ((i = bufferedReader.read()) != -1) {
    5.       bufferedWriter.write(i);
    6.       bufferedWriter.flush();
    7.   }
    8.   bufferedWriter.close();
    9.   bufferedReader.close();
    复制代码
  • 字符缓冲输入输出流:一次读写一个字符数组。
    1.   BufferedReader bufferedReader = new BufferedReader(new FileReader("src/com/shujia/data/a.txt"));
    2.   BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("src/com/shujia/data/b.txt"));
    3.   char[] chars = new char[1024];
    4.   int length = 0;
    5.   while ((length = bufferedReader.read(chars)) != -1) {
    6.       bufferedWriter.write(chars,0,length);
    7.       bufferedWriter.flush();
    8.   }
    9.   bufferedWriter.close();
    10.   bufferedReader.close();
    复制代码
  • 字符缓冲输入输出流:一次读写一行数据。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

千千梦丶琪

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表