IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

Java Properties集合基础解析

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

Java Properties集合基础解析

本期学习的properties集合是项目中经常用到的操作

什么是Properties集合?

java.util.Properties集合继承于Hashtable,来表示一个持久的属性集,他使用键值结构存储数据,每个键及其对应的值都是一个字符串,该类被许多java类使用,比如获取系统属性时,System.getProperties,方法就是返回一个Properties对象

properties集合是唯一一个与IO流相结合的集合
可以使用Properties集合中的方法store把集合中的数据持久化
可以使用Properties集合中的load方法,把硬盘中保存的文件(键值对)存储到集合中使用,这在项目中 用于读取配置文件经常使用到

属性表中每个键及其对应值都是一个字符串
Properties集合是一个双列集合(双列集合是每个元素由键和值两部分组成,由键可以找到值,键必须是唯一的,值可以重复)

构造方法

public properties() 创建一个空的属性列表

基本的存储方法:
public Object setProperty(String key,String value) :保存一对属性
public String setProperties(String key) :使用此属性列表中指定的键搜索属性值
public Set stringPropertNames() 获取所有名称的集合

public class Main {
    public static void main(String[] args) {
        Properties properties = new Properties();
        //存入键值对
        properties.setProperty("one","1");
        properties.setProperty("two","2");
        //拿出所有键
        Set<String> strings = properties.stringPropertyNames();
        //遍历键
        for (String string : strings) {
            //通过键获取值
            String property = properties.getProperty(string);
            //输出
            System.out.println(string+":"+property);
        }
    }
}

结果
130_1.png

与流相关的方法

store ( OutputStream out, String comments)

store ( OutputStream out, String comments) : 以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 – 值对写入到指定的文件中去。

参数中使用了字节输入流,通过流对象,可以关联到某文件上,这样就可以能够加载文本中的数据了,文本数据格式:

public class Main {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        //存入键值对
        properties.setProperty("one","1");
        properties.setProperty("two","2");
        //加载文本中信息到属性集
        properties.store(new FileWriter("c.text"),"savedate");
        //拿出所有键
        Set<String> strings = properties.stringPropertyNames();
    }
}


结果

130_2.png

public void load(InputStream inStream)

public void load(InputStream inStream) : 从字节输入流中读取键值对。

注意:
1、存储键值对的文件中,键与值默认的链接符号可以使用=,空格等其他符号
2、存储键值对的文件中,可以使用“#”进行注释,被注释的键值对默认不会被读取
3、存储键值对的文件中,键与值都是字符串,不要加引号

public class Main {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        properties.load(new FileReader("c.text"));
        //拿出所有键
        Set<String> strings = properties.stringPropertyNames();
        for (String string : strings) {
            System.out.println(string+":"+properties.getProperty(string));
        }
    }
}

结果

130_3.png

以上就是Properties集合的一些基础知识,如有错误请各位批评指正,喜欢我的文章可以点赞收藏,我会不定期更新文章,各位道友也可以关注我

130_4.png

文章永久链接:https://tech.souyunku.com/?p=38833


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(61) 打赏



未经允许不得转载:搜云库技术团队 » Java Properties集合基础解析

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367