属性文件的使用 [英] Use of Property file
问题描述
我正在为数据库连接创建属性文件..我已经给出了文件的路径,我正在使用函数存储属性:
public void setProperty(String key, String value) {properties.setProperty(key, value);}
我已经设置了如下属性:
"url","jdbc:mysql://localhost:3306/";"dbname","example";"user", "postgres";"Pass","123";
我的问题是,当我在另一个系统中使用我的应用程序时,这个属性文件如何有用(实际上我已经阅读了java 中的属性文件"教程,但我无法理解它们是如何使用的).>
在新系统中用户名和密码会不同,那么这个文件怎么用??
在新系统中用户名和密码会不同,那么这个文件怎么用??
它很有用正是因为该文件在不同的系统上是不同的.通过将特定于系统的详细信息放入一个可以在不同系统上更改的文件中,您就不必为每个不同的系统编写不同的 Java 代码.
<块引用>但是我怎么知道其他用户的用户名和密码
在你不知道的情况下,你有多种选择,例如:
- 告诉用户使用文本编辑器编辑属性文件,
- 对应用程序的安装程序进行编码以询问用户名和密码,然后在安装时将它们插入到属性文件中,或者
- 提供允许用户输入或更新属性的配置工具或界面.
顺便说一下,您似乎发明了一种自定义属性文件格式.你不需要这样做.java.util.Properties
类提供了两种标准格式和方法来读取和写入它们.
I am creating properties file for database connection..I have given the path of the file and I am storing properties using the function:
public void setProperty(String key, String value) {
properties.setProperty(key, value);
}
I have set the properties like:
"url","jdbc:mysql://localhost:3306/";"dbname","example";"user", "postgres";"Pass","123";
My question is when I am using my application in another system how could this properties file is useful (Actually I have gone through the "property file in java" tutorials but I was unable to understand how they are used).
In new system user name and password would be different,so how could this file be useful??
In new system user name and password would be different,so how could this file be useful??
It is useful precisely because the file can be different on different systems. By putting the system-specific details into a file that can be changed on different systems, you avoid having to write different Java code for each different system.
But how could I know the other users user name and password
In the scenario where you don't know, you have a number of alternatives, such as:
- telling the user to edit the properties file with a text editor,
- coding your application's installer to ask the for the username and password and then insert them into the properties file at install time, or
- providing a configuration tool or interface that allows the user to enter or update the properties.
By the way, it looks like you have invented a custom property file format. You didn't need to do that. The java.util.Properties
class offers two standard formats and methods for reading and writing them.
这篇关于属性文件的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!