学习目标

  • File类
    • File类的作用
    • File类的常用功能
  • 字节流
    • IO流的作用及分类
    • 字节流读写数据
    • 字节缓冲流读写数据
  • Properties集合
    • Properties集合的介绍
    • Properties集合和IO相关功能
    • Properties集合集合流操作
  • 递归

    • 递归思想
    • 递归案例

      1. File类

  • 绝对路径:以盘符开始

  • 相对路径:没有盘符

    1.1 构造方法

    1. //File重写了toString,File将文件和目录封装成对象
    2. // 1 File(String pathname) 通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例
    3. File file = new File("D:\\abc");
    4. // 2 File(String parent, String child) 从父路径名字符串和子路径名字符串创建新的 File实例
    5. File file1 = new File("D:\\abc","a.txt");
    6. //3 File(File parent, String child) 从父抽象路径名和子路径名字符串创建新的 File实例
    7. File file2 = new File(new File("D:\\abc"),"a.txt");

    1.2 File类的创建和删除功能

    1. //2 public boolean mkdir():创建一个单级文件夹
    2. File file = new File("day10_demo\\cc");
    3. //System.out.println(file.mkdir());
    4. //1 public boolean createNewFile(): 创建一个新的空的文件
    5. File file1 = new File("day10_demo\\b.txt");
    6. System.out.println(file1.createNewFile());
    7. //3 public boolean mkdirs() :创建一个多级文件夹
    8. File file2 = new File("day10_demo\\aa\\bb\\cc");
    9. //System.out.println(file2.mkdirs());
    1. public boolean delete() 删除由此抽象路径名表示的文件或目录
    2. 注意 :
    3. 1 delete方法直接删除不走回收站。
    4. 2 如果删除的是一个文件,直接删除。
    5. 3 如果删除的是一个文件夹,空文件夹直接删掉 , 有内容文件夹需要先删除子文件

    1.3 File类的判断和获取功能

    1. File类判断和获取功能:
    2. public boolean isDirectory() 测试此抽象路径名表示的File是否为目录
    3. public boolean isFile() 测试此抽象路径名表示的File是否为文件
    4. public boolean exists() 测试此抽象路径名表示的File是否存在
    5. public String getAbsolutePath() 返回此抽象路径名的绝对路径名字符串
    6. public String getPath() 获取的是创建File对象给定的路径
    7. public String getName() 返回由此抽象路径名表示的文件或目录的名称

    1.4 File类的高级获取功能

    1. File file = new File("D:\\");
    2. //public File[] listFiles() 返回此抽象路径名表示的目录中的文件和目录的File对象数组
    3. File[] files = file.listFiles();
    4. for (File f : files) {
    5. System.out.println(f.getName());
    6. }
    7. listFiles方法注意事项:
    8. 1 当调用者不存在时,返回null
    9. 2 当调用者是一个文件时,返回null
    10. 3 当调用者是一个空文件夹时,返回一个长度为0的数组
    11. 4 当调用者是一个有内容的文件夹时,将里面所有文件和文件夹的路径放在File数组中返回 , 拿到儿子辈的文件 , 包含隐藏内容

    image.png

    2. IO流

    2.1 IO流能解决什么问题?

    在以往的学习中,我们通过变量,数组或者集合存储的数据,都是存储在内存中的,不能永久化存储,只要代码运行结束数据就会丢失,所以我们需要使用IO流,将数据写到文件中,实现数据永久化存储,还可以把文件中的数据读取到内存中。在网络中数据的传输。

    2.2 IO流分类(以内存为中心)

  • 按照流向区分

    • 输入流 : 用来读数据
    • 输出流 : 用来写数据
  • 按照类型区分
    • 字节流(可以操作任意文件)
    • 字符流(只能操作纯文本文件,用windows记事本打开能读的懂,那么这样的文件就是纯文本文件)

      2.3 字节流

      2.3.1 字节输出流

      ```java //public FileOutputStream(File file) : 往file类型的路径中写入数据 File file = new File(“day10_demo\a.txt”); //1.创建字节输出流对象 FileOutputStream outputStream = new FileOutputStream(file); //public FileOutputStream(String name) : 往String类型的路径中写入数据 FileOutputStream outputStream = new FileOutputStream(“day10_demo\b.txt”);
  1. // 1 void write(int b) 一次写一个字节数据
  2. //写入数据
  3. fos.write(99);
  4. fos.write(100);
  5. fos.write(101);
  6. //2 void write(byte[] b) 一次写一个字节数组数据
  7. //定义一个数组
  8. byte[] bys = {65, 66, 67, 68, 69};
  9. fos.write(bys);
  10. //3 void write(byte[] b, int off, int len) 一次写一个字节数组的部分数据
  11. byte[] bys = {65, 66, 67, 68, 69};
  12. fos.write(bys,1,2);//2代表读取长度
  13. //换行
  14. fos.write(97);
  15. //由于字节流无法写入一个字符串,把字符串转成字节数组写入
  16. fos.write("\r\n".getBytes());
  17. //追加数据 通过构造方法 : public FileOutputStream(String name,boolean append)
  1. <a name="qj0n9"></a>
  2. #### 2.3.2 字节输入流
  3. ```java
  4. public class FileInputStreamDemo7 {
  5. public static void main(String[] args) throws IOException {
  6. //创建输入流对象
  7. FileInputStream fis = new FileInputStream("D:\\ljh\\Pictures\\Saved Pictures\\均衡模式.jpg");
  8. //创建输出流对象
  9. FileOutputStream fos = new FileOutputStream("day10_demo\\copy.jpg");
  10. //创建一个数组
  11. byte[] bytes = new byte[1024];
  12. int len;//每次读到数据的个数
  13. //读数据
  14. while ((len= fis.read(bytes))!=-1){
  15. fos.write(bytes,0,len);//写数据
  16. }
  17. fis.close();
  18. fos.close();
  19. }
  20. }

2.3.3 字节缓冲流如何使用 ?

  1. public class BufferedStreamDemo1 {
  2. public static void main(String[] args) throws IOException {
  3. //创建高效的字节输入流
  4. BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\ljh\\Pictures\\Saved Pictures\\均衡模式.jpg"));
  5. //创建高效的字节输出流
  6. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day10_demo\\b.jpg"));
  7. //一次读一个一个字节
  8. /* int by;
  9. while ((by=bis.read())!=-1){
  10. bos.write(by);
  11. }*/
  12. //一次读一个数组
  13. byte[] bytes = new byte[1024];
  14. int len;//每次真实读到数据的个数
  15. while ((len=bis.read(bytes))!=-1){
  16. bos.write(bytes,0,len);
  17. }
  18. //释放资源
  19. bis.close();
  20. bos.close();
  21. }
  22. }

3. Properties集合

3.1 Properties集合特有的方法 ?
  1. public class PropertiesDemo2 {
  2. public static void main(String[] args) {
  3. Properties p = new Properties();
  4. //Object setProperty(String key, String value) 设置集合的键和值,都是String类型,相当于put方法
  5. p.setProperty("aa","11");
  6. p.setProperty("bb","22");
  7. p.setProperty("cc","33");
  8. //Set<String> stringPropertyNames() 从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串 , 相当于keySet方法
  9. Set<String> stringSet = p.stringPropertyNames();
  10. for (String key : stringSet) {
  11. //String getProperty(String key) 使用此属性列表中指定的键搜索属性 , 相当于get方法
  12. Object value = p.getProperty(key);
  13. System.out.println(key+":"+value);
  14. }
  15. }
  16. }

3.2 Properties集合和IO相关的功能 ?
  1. public class PropertiesDemo3 {
  2. public static void main(String[] args) throws IOException {
  3. //创建集合对象
  4. Properties p = new Properties();
  5. p.setProperty("aa","11");
  6. p.setProperty("bb","22");
  7. p.setProperty("cc","33");
  8. //void store(OutputStream out, String comments) 把集合中的键值对,以字节流形式写入文件中 , 参数二为注释
  9. p.store(new FileOutputStream("day10_demo\\a.txt"),"AAA");
  10. //void load(InputStream inStream) : 以字节流形式 , 把文件中的键值对, 读取到集合中
  11. p.load(new FileInputStream("day10_demo\\a.txt"));
  12. System.out.println(p);
  13. }
  14. }

4. ResourceBundle加载属性文件

  1. public class ResourceBundleDemo {
  2. public static void main(String[] args) {
  3. //1.属性集名称不含扩展名。
  4. //2.属性集文件是在src目录中的
  5. //
  6. ResourceBundle bundle = ResourceBundle.getBundle("user");
  7. //通过键,获取对应的值
  8. String key = bundle.getString("username");
  9. String value = bundle.getString("password");
  10. System.out.println(key);
  11. System.out.println(value);
  12. }
  13. }