简体   繁体   English

如何使用Java中的记事本在项目外部调用database.properties?

[英]How to call database.properties outside the project using notepad in java?

I want to take place database.properties outside the project, so when I want to change the content (database configuration) of that when I've build them into jar , I can do it easily without open my project again. 我想在项目外部放置database.properties,所以当我将其内容构建到jar时要更改其内容(数据库配置)时,无需再次打开项目就可以轻松地做到这一点。 So what to do? 那么该怎么办?

First, place the database.properties file in the location you'd like it to be in. 首先,将database.properties文件放置在您想要的位置。

Then, do one of the following: 然后,执行下列操作之一:

  1. Add the directory where database.properties is located, to the classpath. database.properties所在的目录添加到类路径。 Then use Thread.currentThread().getContextClassLoader().getResource() to get a URL to the file, or getResourceAsStream() to get an input stream for the file. 然后使用Thread.currentThread().getContextClassLoader().getResource()获取文件的URL,或使用getResourceAsStream()获取文件的输入流。
  2. If you don't mind your Java application knowing the exact location of the database.properties file, you can use simple File I/O to obtain a reference to the file (use new File(filename) ). 如果您不介意Java应用程序知道database.properties文件的确切位置,则可以使用简单的File I / O获得对该文件的引用(使用new File(filename) )。

Usually, you'd want to stick with the first option. 通常,您会选择第一种选择。 Place the file anywhere, and add the directory to the classpath. 将文件放在任何位置,然后将目录添加到类路径。 That way, your Java application doesn't have to be aware of the exact location of the file - it will find it as long as the file's directory is added to the runtime classpath. 这样,您的Java应用程序不必知道文件的确切位置-只要将文件目录添加到运行时类路径中,它就会找到它。

Example (for the first approach): 示例(第一种方法):

public static void main(String []args) throws Exception {
    InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("database.properties");
    Properties props = new Properties();

    try {
        // Read the properties.
        props.load(stream);
    } finally {
        // Don't forget to close the stream, whatever happens.
        stream.close();
    }

    // When reaching this point, 'props' has your database properties.
}

Store properties file in your preferred location. 将属性文件存储在您的首选位置。 Then do the following: 然后执行以下操作:

try {

    String myPropertiesFilePath = "D:\\configuration.properties"; // path to your properties file
    File myPropFile = new File(myPropertiesFilePath); // open the file

    Properties theConfiguration = new Properties();
    theConfiguration.load(new FileInputStream(myPropFile)); // load the properties

catch (Exception e) {

}

Now you can easily get properties as String from the file: 现在,您可以轻松地从文件中获取字符串形式的属性:

String datasourceContext = theConfiguration.getString("demo.datasource.context", "jdbc/demo-DS"); // second one is the default value, in case there is no property defined in the file

Your configuration.properties file might look something like this: 您的configuration.properties文件可能如下所示:

demo.datasource.context=jdbc/demo-DS
demo.datasource.password=123

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 处理java项目中的每个开发人员资源,例如database.properties或log4j.properties - Dealing with per-developer resources such as database.properties or log4j.properties in a java project 是否可以从Hibernate中的项目结构外部访问database.properties文件? - Is it possible to access database.properties file from outside of project structure in Hibernate? 如何覆盖jar中捆绑的database.properties - How to override the database.properties bundled in the jar 仅在Intellij中运行的resthub自举式Maven Java项目中的生产中使用自定义database.properties文件 - Use a custom database.properties file only in production in a resthub bootstrapped maven java project running in Intellij 将spring.properties从spring项目包含到web.xml中 - include database.properties from spring project into web.xml java.io.FileNotFoundException:无法打开ServletContext资源[/resources/properties/database.properties] - java.io.FileNotFoundException: Could not open ServletContext resource [/resources/properties/database.properties] Java初学者-我应该在哪个文件夹中放置“ database.properties”文件 - java beginner- in which folder should I place a “database.properties” file 尝试部署服务器时找不到 database.properties - Cannot find database.properties when trying to deploy server 在哪里将activejdbc.properties文件放在ActiveJDBC中以引用database.properties文件? - Where to put the activejdbc.properties file for referencing the database.properties file in ActiveJDBC? 有没有一个很好的理由让Spring Roo将database.properties放入META-INF / spring? - Is there a good reason why Spring Roo puts database.properties in META-INF/spring?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM