java读取properties文件完整指南

在Java应用程序中,经常需要读取properties配置文件中的参数,以便配置应用程序的运行参数。读取properties可以使用Java的 Properties 类,该类可以很方便地读取配置文件中的属性。

一、读取properties文件基础

首先,我们需要先创建一个 properties 文件,包含需要读取的属性和值:

name=张三
age=25
gender=男

然后,我们需要使用 Properties 类来读取配置文件。我们可以使用 Properties 类提供的 load() 方法来加载 properties 文件。

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ReadProperties {
    public static void main(String[] args) {
        // 获取配置文件路径
        String configPath = "config.properties";
        // 创建 Properties 对象
        Properties props = new Properties();
        // 使用 ClassLoader 加载配置文件
        InputStream in = ReadProperties.class.getClassLoader().getResourceAsStream(configPath);
        try {
            // 加载配置文件
            props.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 获取属性值
        String name = props.getProperty("name");
        String age = props.getProperty("age");
        String gender = props.getProperty("gender");
        System.out.println("name=" + name);
        System.out.println("age=" + age);
        System.out.println("gender=" + gender);
    }
}

二、读取properties文件的其他方式

1. 使用 Properties 类提供的 load(InputStream) 方法

我们可以使用 Properties 类提供的 load(InputStream) 方法,直接从 InputStream 中读取配置文件。

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ReadProperties {
    public static void main(String[] args) {
        // 获取配置文件路径
        String configPath = "config.properties";
        // 创建 Properties 对象
        Properties props = new Properties();
        // 使用 FileInputStream 加载配置文件
        InputStream in = null;
        try {
            in = new FileInputStream(configPath);
            // 加载配置文件
            props.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 获取属性值
        String name = props.getProperty("name");
        String age = props.getProperty("age");
        String gender = props.getProperty("gender");
        System.out.println("name=" + name);
        System.out.println("age=" + age);
        System.out.println("gender=" + gender);
    }
}

2. 使用 Properties 类提供的 loadFromXML(InputStream) 方法

除了可以读取 properties 配置文件之外,Properties 类还提供了读取 XML 格式的配置文件的方法。

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ReadProperties {
    public static void main(String[] args) {
        // 获取配置文件路径
        String configPath = "config.xml";
        // 创建 Properties 对象
        Properties props = new Properties();
        // 使用 FileInputStream 加载配置文件
        InputStream in = null;
        try {
            in = new FileInputStream(configPath);
            // 加载配置文件
            props.loadFromXML(in);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 获取属性值
        String name = props.getProperty("name");
        String age = props.getProperty("age");
        String gender = props.getProperty("gender");
        System.out.println("name=" + name);
        System.out.println("age=" + age);
        System.out.println("gender=" + gender);
    }
}

3. 使用 ResourceBundle 类读取配置文件

另一种读取配置文件的方式是使用 ResourceBundle 类。通过 ResourceBundle 类,我们可以读取多种类型的配置文件,如 properties、XML、txt 等等。

import java.util.ResourceBundle;

public class ReadProperties {
    public static void main(String[] args) {
        // 获取配置文件路径
        String configPath = "config";
        // 创建 ResourceBundle 对象
        ResourceBundle bundle = ResourceBundle.getBundle(configPath);
        // 获取属性值
        String name = bundle.getString("name");
        String age = bundle.getString("age");
        String gender = bundle.getString("gender");
        System.out.println("name=" + name);
        System.out.println("age=" + age);
        System.out.println("gender=" + gender);
    }
}

三、读取properties文件中的数组

在 properties 配置文件中,可以将多个值用逗号隔开存储,以实现读取数组的效果。

fruits=apple,banana,orange

在读取数组时,我们需要先使用 getProperty() 方法获取一个包含了所有值的字符串,然后再使用 split() 方法分割字符串,得到一个字符串数组。

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Properties;

public class ReadPropertiesArray {
    public static void main(String[] args) {
        // 获取配置文件路径
        String configPath = "config.properties";
        // 创建 Properties 对象
        Properties props = new Properties();
        // 使用 ClassLoader 加载配置文件
        InputStream in = ReadPropertiesArray.class.getClassLoader().getResourceAsStream(configPath);
        try {
            // 加载配置文件
            props.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 获取属性值
        String fruitsStr = props.getProperty("fruits");
        String[] fruits = fruitsStr.split(",");
        System.out.println(Arrays.toString(fruits));
    }
}

四、读取properties文件中的特殊字符

在 properties 配置文件中,有时候需要使用特殊字符,如空格、等号、冒号等等。这些特殊字符在 properties 文件中需要进行转义。

在 Java 中,可以使用 Unicode 转义进行转义。例如:空格字符可以写成 \u0020。

name=John\u0020Doe
password=my\u0020password

在读取特殊字符时,Properties 类会自动解析 Unicode 转义字符。

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ReadPropertiesSpecialChar {
    public static void main(String[] args) {
        // 获取配置文件路径
        String configPath = "config.properties";
        // 创建 Properties 对象
        Properties props = new Properties();
        // 使用 ClassLoader 加载配置文件
        InputStream in = ReadPropertiesSpecialChar.class.getClassLoader().getResourceAsStream(configPath);
        try {
            // 加载配置文件
            props.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 获取属性值
        String name = props.getProperty("name");
        String password = props.getProperty("password");
        System.out.println("name=" + name);
        System.out.println("password=" + password);
    }
}

五、读取properties文件中的中文

在 properties 配置文件中,可以使用中文字符。但是,这些中文字符在文件中是以 Unicode 编码进行存储的。

在读取中文字符时,Properties 类会自动解析 Unicode 编码,将其转换成中文字符。

name=\u5f20\u4e09
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ReadPropertiesChinese {
    public static void main(String[] args) {
        // 获取配置文件路径
        String configPath = "config.properties";
        // 创建 Properties 对象
        Properties props = new Properties();
        // 使用 ClassLoader 加载配置文件
        InputStream in = ReadPropertiesChinese.class.getClassLoader().getResourceAsStream(configPath);
        try {
            // 加载配置文件
            props.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 获取属性值
        String name = props.getProperty("name");
        System.out.println("name=" + name);
    }
}

六、总结

本文介绍了如何使用 Properties 类读取配置文件。读取配置文件可以使用不同的方式,包括使用 Properties 类提供的 load()、load(InputStream) 和 loadFromXML(InputStream) 方法、使用 ResourceBundle 类等等。同时,读取properties文件中的数组、特殊字符、中文字符也有一定的技巧。

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/297550.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-28 12:16
下一篇 2024-12-28 12:16

相关推荐

  • java client.getacsresponse 编译报错解决方法

    java client.getacsresponse 编译报错是Java编程过程中常见的错误,常见的原因是代码的语法错误、类库依赖问题和编译环境的配置问题。下面将从多个方面进行分析…

    编程 2025-04-29
  • Java JsonPath 效率优化指南

    本篇文章将深入探讨Java JsonPath的效率问题,并提供一些优化方案。 一、JsonPath 简介 JsonPath是一个可用于从JSON数据中获取信息的库。它提供了一种DS…

    编程 2025-04-29
  • Java Bean加载过程

    Java Bean加载过程涉及到类加载器、反射机制和Java虚拟机的执行过程。在本文中,将从这三个方面详细阐述Java Bean加载的过程。 一、类加载器 类加载器是Java虚拟机…

    编程 2025-04-29
  • Java腾讯云音视频对接

    本文旨在从多个方面详细阐述Java腾讯云音视频对接,提供完整的代码示例。 一、腾讯云音视频介绍 腾讯云音视频服务(Cloud Tencent Real-Time Communica…

    编程 2025-04-29
  • 运维Python和GO应用实践指南

    本文将从多个角度详细阐述运维Python和GO的实际应用,包括监控、管理、自动化、部署、持续集成等方面。 一、监控 运维中的监控是保证系统稳定性的重要手段。Python和GO都有强…

    编程 2025-04-29
  • Python应用程序的全面指南

    Python是一种功能强大而简单易学的编程语言,适用于多种应用场景。本篇文章将从多个方面介绍Python如何应用于开发应用程序。 一、Web应用程序 目前,基于Python的Web…

    编程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介绍

    本文将详细介绍Java Milvus SearchParam withoutFields的相关知识和用法。 一、什么是Java Milvus SearchParam without…

    编程 2025-04-29
  • Python wordcloud入门指南

    如何在Python中使用wordcloud库生成文字云? 一、安装和导入wordcloud库 在使用wordcloud前,需要保证库已经安装并导入: !pip install wo…

    编程 2025-04-29
  • vue下载无后缀名的文件被加上后缀.txt,有后缀名的文件下载正常问题的解决

    本文旨在解决vue下载无后缀名的文件被加上后缀.txt,有后缀名的文件下载正常的问题,提供完整的代码示例供参考。 一、分析问题 首先,需了解vue中下载文件的情况。一般情况下,我们…

    编程 2025-04-29
  • 如何在Java中拼接OBJ格式的文件并生成完整的图像

    OBJ格式是一种用于表示3D对象的标准格式,通常由一组顶点、面和纹理映射坐标组成。在本文中,我们将讨论如何将多个OBJ文件拼接在一起,生成一个完整的3D模型。 一、读取OBJ文件 …

    编程 2025-04-29

发表回复

登录后才能评论