13. IO流

作者: 星野君 | 来源:发表于2022-04-29 10:28 被阅读0次

    一、文件
    创建文件:

    public class Home {
      public static void main(String[] args) {
        String filePath = "e:\\news1.txt";
        File file = new File(filePath);
        try {
          file.createNewFile();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    

    二、IO流分类

    image.png
    image.png

    三、节点流FileInputStream
    文件字节输入流:

    public class Home {
      public static void main(String[] args) {
        String filePath = "e:\\a.txt";
        byte[] buf = new byte[8];
        int readLen = 0;
        FileInputStream fileInputStream = null;
        try {
          fileInputStream = new FileInputStream(filePath);
          while ((readLen = fileInputStream.read(buf)) != -1) {
            System.out.print(new String(buf, 0, readLen));
          }
    
        } catch (IOException e) {
          e.printStackTrace();
        }finally{
          try {
            fileInputStream.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
    }
    

    四、节点流FileOutputStream
    文件字节输出流:

    public class Home {
      public static void main(String[] args) {
        String filePath = "e:\\a.txt";
        FileOutputStream  fileOutputStream =null;
        try {
          // 参数加true是追加内容,不加是覆盖
          fileOutputStream = new FileOutputStream(filePath);
          String str = "hello,world!";
          fileOutputStream.write(str.getBytes());
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          try {
            fileOutputStream .close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
    }
    

    四、节点流FileReader
    文件字符输入流:

    public class Home {
      public static void main(String[] args) {
        String filePath = "e:\\a.txt";
        FileReader fileReader = null;
        int readLen = 0;
        char[] buf = new char[8];
        try {
          fileReader = new FileReader(filePath);
          while ((readLen = fileReader.read(buf)) != -1) {
            System.out.print(new String(buf, 0, readLen));
          }
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          try {
            fileReader.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
    }
    

    五、节点流FileWriter
    文件字符输出流:

    public class Home {
      public static void main(String[] args) {
        String filePath = "e:\\a.txt";
        FileWriter fileWriter = null;
        try {
          fileWriter = new FileWriter(filePath);
          fileWriter.write(" 你好北京~");
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          try {
            fileWriter.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
    }
    

    六、处理流-BufferedReader
    字符流输入流:

    public class Home {
      public static void main(String[] args) {
        String filePath = "e:\\a.txt";
        String line;
        BufferedReader bufferedReader = null;
        try {
          bufferedReader = new BufferedReader(new FileReader(filePath));
          while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
          }
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          try {
            bufferedReader.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
    }
    

    七、处理流-BufferedWriter
    字符流输出流:

    public class Home {
      public static void main(String[] args) {
        String filePath = "e:\\a.txt";
        BufferedWriter bufferedWriter = null;
        try {
          bufferedWriter = new BufferedWriter(new FileWriter(filePath));
          bufferedWriter.write("哈哈哈");
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          try {
            bufferedWriter.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
    }
    

    八、处理流-BufferedInputStream
    字节输入流:

    public class Home {
      public static void main(String[] args) {
        String filePath = "e:\\a.txt";
        BufferedInputStream bufferedInputStream = null;
        byte[] buff = new byte[1024];
        try {
          bufferedInputStream = new BufferedInputStream(new FileInputStream(filePath));
          while (bufferedInputStream.read(buff) != -1) {
            System.out.println(new String(buff));
          }
    
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          try {
            bufferedInputStream.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
    }
    

    九、处理流-BufferedOutputStream
    字节输出流:

    public class Home {
      public static void main(String[] args) {
        String filePath = "e:\\a.txt";
        BufferedOutputStream bufferedOutputStream = null;
        try {
          bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(filePath));
          bufferedOutputStream.write("xxx".getBytes());
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          try {
            bufferedOutputStream.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
    }
    

    十、对象流-ObjectInputStream

    public class Home {
      public static void main(String[] args) {
        String filePath = "e:\\a.dat";
        ObjectInputStream objectInputStream = null;
        try {
          objectInputStream = new ObjectInputStream(new FileInputStream(filePath));
          System.out.println(objectInputStream.readObject()); // 这里返回的是object对象
    
        } catch (IOException | ClassNotFoundException e) {
          e.printStackTrace();
        } finally {
          try {
            objectInputStream.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
    }
    
    class Cat implements Serializable {
      private static final long serialVersionUID = 1L;
      private String name;
    
      public Cat(String name) {
        this.name = name;
      }
    
      public String getName() {
        return name;
      }
    
      public void setName(String name) {
        this.name = name;
      }
    }
    

    十一、对象流ObjectOutputStream

    public class Home {
      public static void main(String[] args) {
        String filePath = "e:\\a.dat";
        ObjectOutputStream oos = null;
        try {
          oos = new ObjectOutputStream(new FileOutputStream(filePath));
          oos.writeObject(new Cat("小黄"));
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          try {
            oos.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
    }
    
    class Cat implements Serializable {
      private static final long serialVersionUID = 1L;
      private String name;
    
      public Cat(String name) {
        this.name = name;
      }
    
      public String getName() {
        return name;
      }
    
      public void setName(String name) {
        this.name = name;
      }
    }
    
    1. 序列化的类必须实现Serializable接口
    2. 读写顺序要一致
    3. 序列化中的类建议添加

    十二、Properties
    Properties是专门用于读写配置文件的集合类。格式是key=value

    常见方法:

    1. load 加载数据到properties对象
    2. list 将数据显示到指定设备
    3. getProperty(key) 根据key获取到value
    4. setProperty(key, value)设置键值对到properties对象
    5. store 将properties对象保存的键值对存储到配置文件中
    //1. 创建 Properties 对象
    Properties properties = new Properties();
    //2. 加载指定配置文件
    properties.load(new FileReader("src\\mysql.properties"));
    //3. 把 k-v 显示控制台
    properties.list(System.out);
    //4. 根据 key 获取对应的值
    String user = properties.getProperty("user");
    String pwd = properties.getProperty("pwd");
    

    相关文章

      网友评论

          本文标题:13. IO流

          本文链接:https://www.haomeiwen.com/subject/fklysrtx.html