Java IO流补充 - Properties - IO流框架commons-io

IO流补充知识

Properties结合IO流集合

我们先来认识Properties属性集对象

Properties其实就是一个Map集合,但是我们一般不会当集合使用,因为HashMap更好用。

在这里插入图片描述

Properties核心作用:

属性文件:后缀是.properties结尾的文件,里面的内容都是 key=value,后续做系统配置信息的。

Properties代表的是一个属性文件,可以把自己对象中的键值对信息存入到一个属性文件中去。

Properties结合IO流的API:

方法 说明
load(InputStream inStream) 从输入字节流读取属性列表(键和元素对)
load(Reader reader) 从输入字符流读取属性列表(键和元素对)
store(OutputStream out, String comments) 将此属性列表(键和元素对)写入此 Properties表中,以适合于使用 load(InputStream)方法的格式写入输出字节流
store(Writer writer, String comments) 将此属性列表(键和元素对)写入此 Properties表中,以适合使用 load(Reader)方法的格式写入输出字符流
setProperty(String key, String value) 保存键值对(put)
getProperty(String key) 使用此属性列表中指定的键搜索属性值 (get)
stringPropertyNames() 所有键的名称的集合 (keySet())

演示代码

setProperty方法和store方法演示

public static void main(String[] args) throws Exception {
    
    
    // 创建集合
    Properties properties = new Properties();

    // 使用setProperty方法把键值对信息存入到属性文件当中去
    properties.setProperty("admin", "123123");
    properties.setProperty("chen", "123456");
    properties.setProperty("abc", "123789");

    /**
        参数一: 设置数据要保存到哪个文件路径下
        参数二: 保存时的注释
     */
    properties.store(new FileOutputStream("/Users/chenyq/Documents/user.properties"), "first");
}

load方法, 将一个xxx.properties文件中的键值对加载到properties对象当中

public static void main(String[] args) throws Exception {
    
    
    // 创建一个Properties对象
    Properties properties = new Properties();
    System.out.println(properties); // {}

    /**
        load方法, 加载属性文件到该properties对象当中
        参数: 表示要加载文件的位置
     */
    properties.load(new FileInputStream("/Users/chenyq/Documents/user.properties"));
    System.out.println(properties); // {chen=123456, abc=123789, admin=123123}
}

getProperty方法, 根据key获取值, 返回字符串类型

public static void main(String[] args) throws Exception {
    
    
    Properties properties = new Properties();

    properties.load(new FileInputStream("/Users/chenyq/Documents/user.properties"));
    System.out.println(properties); // {chen=123456, abc=123789, admin=123123}


    // getProperty根据key获取值, 返回字符串类型
    String str = properties.getProperty("admin");
    System.out.println(str); // 123123
}

IO流框架

commons-io概述:

commons-io是apache开源基金组织提供的一组有关IO操作的类库,可以提高IO功能开发的效率。

commons-io工具包提供了很多有关io操作的类。有两个主要的类FileUtils, IOUtils

commons-io其实是第三方的包, 我们如果使用需要导入commons-io包, 导入步骤如下:

  1. 在官网中下载commons-io 下载链接
  2. 在项目中创建一个文件夹:lib, 将commons-io-2.6.jar文件复制到lib文件夹
  3. 在jar文件上点右键,选择 Add as Library -> 点击OK
  4. 完成上面步骤后, 就可以在类中导包使用

我们主要使用FileUtils类下面的方法(其他方法可以参考文档):

方法名 说明
readFileToString(File file, String encoding) 读取文件中的数据, 返回字符串
copyFile(File srcFile, File destFile) 复制文件。
copyDirectoryToDirectory(File srcDir, File destDir) 复制文件夹。
copyFileToDirectory(File srcDir, File destDir) 复制文件到文件夹

copyFile方法演示

public static void main(String[] args) throws Exception {
    
    
    /**
        copyFile方法复制文件
        参数一: 要复制的文件
        参数二: 复制后的文件
     */
    FileUtils.copyFile(new File("/Users/chenyq/Documents/test.txt"), new File("/Users/chenyq/Documents/test2.txt"));
}

copyDirectoryToDirectory方法掩饰

public static void main(String[] args) throws Exception {
    
    
    /**
        copyDirectoryToDirectory方法复制文件夹
        参数一: 要复制的文件或文件夹
        参数二: 要复制的文件夹
     */
    FileUtils.copyDirectoryToDirectory(new File("/Users/chenyq/Documents/test"), new File("/Users/chenyq/Documents/utils"));
}

copyFileToDirectory方法复制文件到文件夹

public static void main(String[] args) throws Exception {
    
    
    /**
        copyFileToDirectory方法复制文件到文件夹
        参数一: 要复制的文件
        参数二: 要复制到的文件夹
     */
    FileUtils.copyFileToDirectory(new File("/Users/chenyq/Documents/test.txt"), new File("/Users/chenyq/Documents/utils"));
}

猜你喜欢

转载自blog.csdn.net/m0_71485750/article/details/127650137
今日推荐