Java IO学习之Properties类

本文详细介绍了Java中的Properties类,它是持久化的属性集合,以键值对形式存储。文章讲解了Properties类的源码、继承关系及常用方法,特别提到了`load()`和`store()`方法在读写属性文件中的应用。此外,还探讨了Java中的路径概念,包括绝对路径和相对路径的差异,以及在不同操作系统下的分隔符。文中举例说明如何使用绝对路径和相对路径读取文件,并推荐了使用`class.getResource()`和`class.getClassLoader().getResource()`来获取资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Properties

1、类源码:Properties 继承 HashTable,属于一个持久的属性集合,以 key-value 键值对形式存在 

class Properties extends Hashtable<Object,Object> {

2、类方法:除了HashTable定义的方法,Properties定义了下列方法,表格来源——菜鸟教程

序号方法描述
1String getProperty(String key)
 用指定的键在此属性列表中搜索属性。
2String getProperty(String key, String defaultProperty)
用指定的键在属性列表中搜索属性。
3void list(PrintStream streamOut)
 将属性列表输出到指定的输出流。
4void list(PrintWriter streamOut)
将属性列表输出到指定的输出流。
5void load(InputStream streamIn) throws IOException
 从输入流中读取属性列表(键和元素对)。
6Enumeration propertyNames( )
按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。
7Object setProperty(String key, String value)
 调用 Hashtable 的方法 put。
8void store(OutputStream streamOut, String description)
 以适合使用  load(InputStream)方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。

注:本篇博客中 data1.properties 路径:/src/data1.propertiesdata2.properties 路径:/src/IO/data2.properties

3、代码示例——写入

package IO;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.Properties;

public class PropertiesDemo {
    public static void main(String[] args) {
        Properties props = new Properties();
        OutputStream os = null;
        //获取PropertiesDemo类的根路径
        URL base = PropertiesDemo.class.getResource("/");
        String filepath = base.getPath() + "IO/data2.properties";
        try {
            os = new FileOutputStream(filepath);
            //键值对
            props.setProperty("key1", "test");
            props.setProperty("key2", "test");
            //保存键值对
            props.store(os, "test");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //关闭输出流
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

4、代码示例——读取

package IO;

import java.io.*;
import java.net.URL;
import java.util.Properties;

public class PropertiesDemo {
    public static void main(String[] args) {
        Properties props = new Properties();
        InputStream in = null;
        //获取PropertiesDemo类的根路径
        URL base = PropertiesDemo.class.getResource("/");
        String filepath = base.getPath() + "IO/data2.properties";
        try {
            //输入流
            in = new FileInputStream(filepath);
            props.load(in);
            System.out.println(props.getProperty("key1"));
        } catch (IOException e) {
            System.out.println("Properties文件读取报错!");
            e.printStackTrace();
        } finally {
            try {
                //关闭输入流
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

5、其他读取方法

  • java.util.ResourceBundle 类的getBundle() 方法
//使用ResourceBundle类的getBundle()方法
ResourceBundle rb=ResourceBundle.getBundle("IO/data2");
//输出key1对应value值
System.out.println(rb.getString("key1"));
  • java.util.PropertyResourceBundle类的构造函数
//java.util.PropertyResourceBundle类的构造函数
ResourceBundle rb=new PropertyResourceBundle(in);
System.out.println(rb.getString("key1"));

注:博主比较常用的还是代码示例中的 props.load(in) 这种方式

二、相对路径和绝对路径

1、概念

  • 绝对路径:文件/目录在硬盘上真正的路径
  • 相对路径:指的是相对JVM的启动路径,其实java实际上都是通过绝对路径寻找资源的,相对路径是API通过底层拼接成绝对路径的。

2、分隔符:java 中通用 System.getProperty(File.separator),System 具备Properties属性

  • Linux 下:" / "
  • Windows 下:" \\ "

3、绝对路径读取文件

  • D:\\1.properties (在windows下,转义为" \\ ")
  • 以src为根目录读取
in=new FileInputStream("src/IO/data2.properties");
props.load(in);

4、相对路径读取文件

  • class.getResource(""):包路径
//包路径是IO,IO下面是data2.properties
String filepath=PropertiesDemo.class.getResource("data2.properties").getPath();
  • class.getResource("/"):classpath根路径
//获取PropertiesDemo类的根路径
URL base = PropertiesDemo.class.getResource("/");
String filepath = base.getPath() + "IO/data2.properties";
String filepath=PropertiesDemo.class.getResource("/data1.properties").getPath();
String filepath=PropertiesDemo.class.getResource("/IO/data2.properties").getPath();
  • class.getClassLoader().getResource(""):包路径
String filepath=PropertiesDemo.class.getClassLoader().getResource("data1.properties").getPath();
String filepath=PropertiesDemo.class.getClassLoader().getResource("IO/data2.properties").getPath();

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值