`

加载Properties文件

Web 
阅读更多
// Load the user settings.
String userSettingsFile = System.getProperty("user.home")+"/user_settings.ini";
BufferedReader in = null;
try {
    in = Utilities.getBufferedReader(userSettingsFile);
    settings.load(in);
    in.close();
} catch (Exception e) {
    /* Program starts with default settings. */
}
finally {
    if(in != null)
    try {
        in.close();
    } catch (IOException e) {}
}

 

/**
 * Returns a <code>BufferedReader</code> to the file corresponding to the passed filename.
 * <p>
 * This method searches for the file in the class path.
 *  
 * @param filename  the name of the file
 * @return the <code>BufferedReader</code> to the file corresponding to the passed filename
 */
public static BufferedReader getBufferedReader(String filename) {

    BufferedReader b = null;

    // Try to read the file, no matter if it is in the class path or not.
    try {
        b = new BufferedReader(new FileReader(filename));	
    } catch (Exception e) {}

    // If the program is started as web start application the file 
    // may be in the jar file.Hence try to read it from the jar, too.
    try {
        if(b == null)
	    b = new BufferedReader(new InputStreamReader(Main.class.getResourceAsStream(filename)));			
    } catch (Exception e) {}
        return b;
}
 
分享到:
评论

相关推荐

    Spring3.0 配置文件中加载Properties文件的小例子

    本篇将详细讲解如何在Spring 3.0的配置文件中加载Properties文件,以便在运行时动态获取和使用这些配置。 首先,我们需要一个Properties文件,例如`application.properties`,它通常放在项目的类路径根目录下。这个...

    Java加载properties文件实现方式详解

    Java加载properties文件实现方式详解 Java加载properties文件是Java开发中常见的场景,properties文件是一种配置文件,用于存储应用程序的配置信息。Java提供了多种方式来加载properties文件,本文将详细介绍Java...

    Java加载properties文件的六种方法

    下面将详细解释标题和描述中提到的六种方法来加载`.properties`文件。 1. **使用 `java.util.Properties` 类的 `load()` 方法** 这是最直接的方法,通过 `FileInputStream` 创建输入流,然后用 `Properties` 类的 ...

    加载src目录下的properties配置文件.docx

    在上面的例子中,我们使用getResourceAsStream()方法将Properties文件加载到InputStream中,然后使用Properties对象加载Properties文件,并将其存储到TreeMap中。 TreeMap TreeMap是一种排序的Map实现,用于存储...

    Python实现读取Properties配置文件的方法

    在Python编程中,有时我们需要处理Java开发中常用的`.properties`配置文件。虽然Python标准库并未直接提供处理此类文件的模块,但我们可以自定义一个类来实现这个功能。本篇文章将详细探讨如何通过Python来读取并...

    详解SpringMVC加载配置Properties文件的几种方式

    本文将详细介绍几种SpringMVC加载Properties文件的方法。 1. 通过`context:property-placeholder`实现配置文件加载 这是最常用的方式,通过在Spring的配置文件(如`spring.xml`)中引入`context`命名空间,并使用`...

    java 读取properties文件代码

    1. **加载Properties文件**:你可以使用`java.util.Properties`类来加载文件。首先,创建一个Properties对象,然后调用`load()`方法加载文件。这通常在类的初始化或静态块中完成。 ```java Properties prop = new ...

    Spring加载properties文件的方法

    在Spring框架中,加载properties文件是常见的配置管理方式,这有助于将应用程序的配置参数与源代码分离,便于维护和更新。本文将详细介绍Spring加载properties文件的两种主要方法:XML方式和注解方式。 ### XML方式...

    静态加载properties配置文件,根据key获取值的方法

    properties文件获取工具类:静态加载properties配置文件,有根据key获取值的方法

    java 改变Properties文件中的键值

    使用`Properties`类的`load()`方法加载Properties文件。这个方法需要一个`InputStream`对象,通常是通过`FileInputStream`来创建的。例如: ```java Properties props = new Properties(); FileInputStream fis ...

    读取properties文件返回map

    3. **加载properties文件** 使用`Properties`类加载`properties`文件有两种主要方法: - `load(InputStream input)`:接受一个`InputStream`,通常是从文件系统、类路径或网络流中获取。 - `load(Reader reader)`...

    Spring加载properties文件的两种方式实例详解

    Spring加载properties文件的两种方式实例详解 在项目中,如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取properties里面的配置,这样后期只需要改动...

    加载properties文件.emmx

    使用mindmaster打开

    properties文件的读取

    2. **加载properties文件** 使用`Properties`类的`load()`方法从输入流中加载文件内容: ```java import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; public class ...

    MyEclipse中Properties文件插件

    MyEclipse提供了便利的工具来读取和管理这些配置,如通过`java.util.Properties`类加载Properties文件,并使用键获取对应的值。 6. 调试与预览 - 在MyEclipse中,可以实时预览Properties文件的效果,这在进行国际...

    SSM 读取properties文件

    这是一个Spring的bean定义类,它允许我们从properties文件中加载和解析属性值,然后将这些值注入到其他bean的属性中。首先,我们需要创建一个properties文件,例如`application.properties`,并放入项目的类路径下...

    图书馆管理系统

    //通过输入流对象加载Properties文件 dbClassName = prop.getProperty("DB_CLASS_NAME"); //获取数据库驱动 dbUrl = prop.getProperty("DB_URL", "jdbc:mysql://127.0.0.1:3306/db_librarySys?user=...

    java读写properties文件,解决系统找不到指定路径,解决写入后读取正常,但文件数据未更新问题

    在Java中,加载Properties文件时需要正确指定其路径。这通常通过以下两种方式完成: 1. 相对类路径:如果你的Properties文件与Java源代码位于同一目录结构下,可以使用`getResourceAsStream()`方法,配合类路径来...

    Java读取properties文件的三种方式

    在这个例子中,我们首先创建一个Properties对象,然后使用`load()`方法从指定的文件路径加载properties文件。`getProperty()`方法用于获取属性值。注意处理可能的IOException。 2. 使用ClassLoader 当我们需要在类...

    java实现properties文件读取

    以下是如何创建和加载Properties文件的基本步骤: 1. **创建Properties对象**: 在Java代码中,我们首先创建一个`Properties`对象,它是读取Properties文件的核心工具。 ```java Properties prop = new ...

Global site tag (gtag.js) - Google Analytics