今日学习目标

  • File类
  • IO流的介绍
  • IO流的分类
  • 字节输出流
  • 字节出入流
  • 字节缓冲流
  • Properties集合
  • ResourceBundle加载属性集合

    1 File类

  • File 类是文件和目录路径名的抽象表示,主要用于文件和目录的创建、查找和删除等操作

    1.2 构造方法

    | 方法 | 作用 | | —- | —- | | File(String pathname) | 通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例 | | File(String parent, String child) | 从父路径名字符串和子路径名字符串创建新的 File实例 | | File(File parent, String child) | 从父抽象路径名和子路径名字符串创建新的 File实例 |

1.3 File类的创建功能

方法 作用
public boolean createNewFile() 创建一个新的空的文件
public boolean mkdir() 创建一个单级文件夹
public boolean mkdirs() 创建一个多级文件夹

1.4 File类的删除功能

方法 作用
public boolean delete() 删除由此抽象路径名表示的文件或目录
  • 注意事项:
    • delete方法直接删除不走回收站.无法恢复
    • 如果删除的是一个文件,直接删除
    • 如果删除的是一个文件夹,需要先删除文件夹中的内容,最后才能删除文件夹

      1.5 File类的判断和获取功能

      | 方法 | 作用 | | —- | —- | | boolean isDirectory() | 测试此抽象路径名表示的File是否为目录 | | boolean isFile() | 测试此抽象路径名表示的File是否为文件 | | boolean exists() | 测试此抽象路径名表示的File是否存在 | | String getAbsolutePath() | 返回此抽象路径名的绝对路径名字符串 | | String getPath() | 将此抽象路径名转换为路径名字符串 | | String getName() | 返回由此抽象路径名表示的文件或目录的名称 | | int length | 返回此文件的文件大小 |

1.6 File类的高级获取功能

方法 作用
public File [ ] listFiles( ) 返回此抽象路径名表示的目录中的文件和目录的File对象数组
  • listFiles方法注意事项:

    • 当调用者不存在时,返回null
    • 当调用者是一个文件时,返回null
    • 当调用者是一个空文件夹时,返回一个长度为0的数组
    • 当调用者是一个有内容的文件夹时,将里面所有文件和文件夹的路径放在File数组中返回
    • 当调用者是一个有隐藏文件的文件夹时,将里面所有文件和文件夹的路径放在File数组中返回,包含隐藏内容

      2 IO流的介绍

      1.1 IO流和平常储存数据的不同

  • 通过变量,数组,或者集合存储数据

    • 都是不能永久化存储 , 因为数据都是存储在内存中
    • 只要代码运行结束,所有数据都会丢失
  • 使用IO流

    • 将数据写到文件中,实现数据永久化存储
    • 把文件中的数据读取到内存中(Java程序)

      1.2 IO流的分类

  • 按照流向区分

    • 输入流是用来读数据的
    • 输出流是用来写数据的
  • 按类型区分
    • 字符流(明天讲)
    • 字节流
  • 注意 :

    • 字节流可以操作任意文件
    • 字符流只能操作纯文本文件(明天讲)
    • 用windows记事本打开不是乱码的,那么这样的文件就是纯文本文件。

      3 字节流输出流

      2.1字节输出流的基础

  • FileOutputStream类:是文件输出流,用于将数据写出到文件

  • 构造方法: | 方法 | 作用 | | —- | —- | | public FileOutputStream(File file) | 创建文件输出流以写入指定的File对象表示的文件 | | public FileOutputStream(String name) | 创建文件输出流以指定的名字写入文件 |

  • 字节输出流写数据

    • 创建字节输出流对象。
    • 写数据
    • 释放资源(关闭 )

      2.1字节输出流的方法

  • 字节流写数据的方法 | 方法 | 作用 | | —- | —- | | void write(int b) | 一次写一个字节数据 | | void write(byte[] b) | 一次写一个字节数组数据 | | void write(byte[] b, int off, int len) | 一次写一个字节数组的部分数据 |

2.2写数据的换行和追加写入

  • 换行符

    • windows : \r\n

      1. linux : \n<br /> mac : \r
  • public FileOutputStream(String name,boolean append)

    • 如果第二个参数为true,不会清空文件里面的内容

      3 字节输入流

      3.1 字节输入流类

  • InputStream类 : 字节输入流最顶层的类 , 抽象类 | 方法 | 作用 | | —- | —- | | public FileInputStream(File file) | 从file类型的路径中读取数据 | | public FileInputStream(String name) | 从字符串路径中读取数据 |

  • 字节输入流步骤:

    • 创建输入流对象
    • 读数据
    • 释放资源
  • public int read(byte[] b) :从输入流读取最多b.length个字节的数据,返回的是真实读到的数据个数

    3.2图片的拷贝

    1. public class FileInputStreamDemo2 {
    2. public static void main(String[] args) throws IOException {
    3. // 创建字节输入流对象
    4. FileInputStream fis = new FileInputStream("D:\\传智播客\\安装包\\好看的图片\\liqin.jpg");
    5. // 创建字节输出流
    6. FileOutputStream fos = new FileOutputStream("day11_demo\\copy.jpg");
    7. // 一次读写一个字节
    8. int by;
    9. while ((by = fis.read()) != -1) {
    10. fos.write(by);
    11. }
    12. // 释放资源
    13. fis.close();
    14. fos.close();
    15. }
    16. }

    3.3 异常的捕获处理

  • JDK7版本优化处理方式 : 自动释放资源

    • 可以使用 try-with-resource 语句,该语句确保了每个资源在语句结束时自动关闭。
  • 格式 :

    • try (创建流对象语句1 ; 创建流对象语句2 …) {

      1. } catch (IOException e) {<br /> 处理异常的代码...<br /> }

      4 缓冲流

      | 方法 | 作用 | | —- | —- | | BufferedOutputStream(OutputStream out) | 缓冲输出流 | | BufferedInputStream(InputStream in) | 缓冲输入流 |

  • 字节缓冲流仅仅提供缓冲区,不具备读写功能 , 而真正的读写数据还得依靠基本的字节流对象进行操作

    4.1四种方式复制视频文件

    ```java import java.awt.image.DataBufferDouble; import java.io.*;

/* 需求:把“xxx.avi”复制到模块目录下的“copy.avi” , 使用四种复制文件的方式 , 打印所花费的时间

  1. 四种方式:
  2. 1 基本的字节流一次读写一个字节 : 花费的时间为:196662毫秒
  3. 2 基本的字节流一次读写一个字节数组 : 花费的时间为:383毫秒
  4. 3 缓冲流一次读写一个字节 : 花费的时间为:365毫秒
  5. 4 缓冲流一次读写一个字节数组 : 花费的时间为:108毫秒
  6. 分析 :
  7. 数据源 : "D:\a.wmv"
  8. 目的地 : "day11_demo\copy.wmv"

*/ public class BufferedStreamDemo2 { public static void main(String[] args) throws IOException { long startTime = System.currentTimeMillis();

  1. // method1();
  2. // method2();
  3. // method3();
  4. method4();
  5. long endTime = System.currentTimeMillis();
  6. System.out.println("花费的时间为:" + (endTime - startTime) + "毫秒");
  7. }
  8. // 4 缓冲流一次读写一个字节数组
  9. private static void method4() throws IOException {
  10. // 创建高效的字节输入流
  11. BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\a.wmv"));
  12. // 创建高效的字节输出流
  13. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day11_demo\\copy.wmv"));
  14. // 一次读写一个字节数组
  15. byte[] bys = new byte[1024];
  16. int len;// 每次真实读到数据的个数
  17. while ((len = bis.read(bys)) != -1) {
  18. bos.write(bys, 0, len);
  19. }
  20. // 释放资源
  21. bis.close();
  22. bos.close();
  23. }
  24. // 3 缓冲流一次读写一个字节
  25. private static void method3() throws IOException {
  26. // 创建高效的字节输入流
  27. BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\a.wmv"));
  28. // 创建高效的字节输出流
  29. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day11_demo\\copy.wmv"));
  30. // 一次读写一个字节
  31. int by;
  32. while ((by = bis.read()) != -1) {
  33. bos.write(by);
  34. }
  35. // 释放资源
  36. bis.close();
  37. bos.close();
  38. }
  39. // 2 基本的字节流一次读写一个字节数组
  40. private static void method2() throws IOException {
  41. // 创建基本的字节输入流对象
  42. FileInputStream fis = new FileInputStream("D:\\a.wmv");
  43. // 创建基本的字节输出流对象
  44. FileOutputStream fos = new FileOutputStream("day11_demo\\copy.wmv");
  45. // 一次读写一个字节数组
  46. byte[] bys = new byte[1024];
  47. int len;// 每次真实读到数据的个数
  48. while ((len = fis.read(bys)) != -1) {
  49. fos.write(bys, 0, len);
  50. }
  51. // 释放资源
  52. fis.close();
  53. fos.close();
  54. }
  55. // 1 基本的字节流一次读写一个字节
  56. private static void method1() throws IOException {
  57. // 创建基本的字节输入流对象
  58. FileInputStream fis = new FileInputStream("D:\\a.wmv");
  59. // 创建基本的字节输出流对象
  60. FileOutputStream fos = new FileOutputStream("day11_demo\\copy.wmv");
  61. // 一次读写一个字节
  62. int by;
  63. while ((by = fis.read()) != -1) {
  64. fos.write(by);
  65. }
  66. // 释放资源
  67. fis.close();
  68. fos.close();
  69. }

}

  1. <a name="EG0KT"></a>
  2. # 5 Properties集合
  3. - properties是一个Map体系的集合类:
  4. - Properties中有跟IO相关的方法
  5. - 不需要加泛型,只用存字符串
  6. <a name="FBGm7"></a>
  7. #### 5.1 Properties作为集合的特有方法
  8. | 方法 | 作用 |
  9. | --- | --- |
  10. | Object setProperty(String key, String value) | 设置集合的键和值,都是String类型,相当于put方法 |
  11. | String getProperty(String key) | 使用此属性列表中指定的键搜索属性 , 相当于get方法 |
  12. | Set<String> stringPropertyNames() | 从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串 , 相当于keySet方法 |
  13. <a name="nGZih"></a>
  14. #### 5.2 properties中和IO相关的方法
  15. | 方法 | 作用 |
  16. | --- | --- |
  17. | void load(InputStream inStream) | 以字节流形式 , 把文件中的键值对, 读取到集合中 |
  18. | void load(Reader reader) | 以字符流形式 , 把文件中的键值对, 读取到集合中 |
  19. | void store(OutputStream out, String comments) | 把集合中的键值对,以字节流形式写入文件中 , 参数二为注释 |
  20. | void store(Writer writer, String comments) | 把集合中的键值对,以字符流形式写入文件中 , 参数二为注释 |
  21. <a name="KoszZ"></a>
  22. ## 6 ResourceBundle加载属性文件
  23. ```java
  24. import java.util.ResourceBundle;
  25. /*
  26. 1 java.util.ResourceBundle : 它是一个抽象类
  27. 我们可以使用它的子类PropertyResourceBundle来读取以.properties结尾的配置文件
  28. 2 static ResourceBundle getBundle(String baseName) 可以根据名字直接获取默认语言环境下的属性资源。
  29. 参数注意: baseName
  30. 1.属性集名称不含扩展名。
  31. 2.属性集文件是在src目录中的
  32. 比如:src中存在一个文件 user.properties
  33. ResourceBundle bundle = ResourceBundle.getBundle("user");
  34. 3 ResourceBundle中常用方法:
  35. String getString(String key) : 通过键,获取对应的值
  36. 优点 : 快速读取属性文件的值
  37. 需求 :
  38. 通过ResourceBundle工具类
  39. 将一个属性文件 放到src目录中,使用ResourceBundle去获取键值对数据
  40. */
  41. public class ResourceBundleDemo {
  42. public static void main(String[] args) {
  43. // public static final ResourceBundle getBundle(String baseName)
  44. // baseName : properties文件的名字 , 注意 : 扩展名不需要加上 , properties必须在src的根目录下
  45. ResourceBundle resourceBundle = ResourceBundle.getBundle("user");
  46. // String getString(String key) : 通过键,获取对应的值
  47. String value1 = resourceBundle.getString("username");
  48. String value2 = resourceBundle.getString("password");
  49. System.out.println(value1);
  50. System.out.println(value2);
  51. }
  52. }
  • 使用ResourceBundle获取属性值:
    • 获取ResourceBundle对象
    • 通过ResourceBundle类中的getString(key) : 根据键找值
  • 使用ResourceBundle加载属性文件,属性文件需要放置在src的根目录