如何在Java中读取属性文件?
我使用servlets,在那里我硬编码数据库连接细节,所以如果做任何更改,我必须重新编译代码。因此,我想使用一个如何在Java中读取属性文件?,java,file,servlets,Java,File,Servlets,我使用servlets,在那里我硬编码数据库连接细节,所以如果做任何更改,我必须重新编译代码。因此,我想使用一个.properties文件(稍后可以修改),并将其用作数据库连接的源 问题是我不知道如何读取属性文件。有人能帮我读一下文件吗?你可以使用属性类有一个方便的加载方法。这是读取java属性文件的最简单方法。读取。通常属性文件保存在类路径中,以便此方法可以读取它 . . . // create and load default properties Properties d
.properties
文件(稍后可以修改),并将其用作数据库连接的源
问题是我不知道如何读取属性文件。有人能帮我读一下文件吗?你可以使用
属性类有一个方便的加载方法。这是读取java属性文件的最简单方法。读取。通常属性文件保存在类路径中,以便此方法可以读取它
. . .
// create and load default properties
Properties defaultProps = new Properties();
FileInputStream in = new FileInputStream("defaultProperties");
defaultProps.load(in);
in.close();
// create application properties with default
Properties applicationProps = new Properties(defaultProps);
// now load properties from last invocation
in = new FileInputStream("appProperties");
applicationProps.load(in);
in.close();
. . .
例子来自这里
Properties
的方法可以抛出异常。
-当文件路径无效时(FileNotFoundException
)。请尝试创建一个文件
对象,并检查文件
是否存在。
-…你可以看一看。使用它,您可以读取如下属性文件:
Configuration config = new PropertiesConfiguration("user.properties");
String connectionUrl = config.getString("connection.url");
有关文件位置的信息可能也很重要:
如果未指定绝对值
路径,将搜索该文件
自动在下面的
地点:
- 在当前目录中
- 在用户主目录中
- 在类路径中
因此,在servlet中读取属性文件时,应该将属性文件放在类路径中(例如,在WEB-INF/classes
中)
您可以在他们的网站上找到更多示例。下面的代码将添加一个侦听器,用于检查使用dbprops系统属性配置的文件。对于每个给定的间隔,它将查看文件是否被修改,如果被修改,它将从文件中加载属性。
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("my.properties");
Properties p = new Properties();
p.load(in);
in.close();
包com.servlets
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class DBPropsWatcherListener
implements ServletContextListener
{
public void contextInitialized(ServletContextEvent event)
{
ServletContext servletContext = event.getServletContext();
Timer timer = new Timer("ResourceListener");
timer.schedule(new MyWatcherTask(servletContext), 15);
}
public void contextDestroyed(ServletContextEvent event)
{
}
private class MyWatcherTask extends TimerTask
{
private final ServletContext servletContext;
private long lastModifiedTime = -1;
public MyWatcherTask(ServletContext servletContext)
{
this.servletContext = servletContext;
}
public void run()
{
try {
File resourceFile = new File(System.getProperty("dbProps"));
long current = resourceFile.lastModified();
if (current > lastModifiedTime) {
java.io.InputStream dbPropsStream = new FileInputStream(resourceFile );
java.util.Properties dbProps = new java.util.Properites();
dbProps.load(dbPropsStream);
realoadDBProps();
}
lastModifiedTime = current;
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
}
从属性文件中读取数据库值是个好主意
您可以使用Util包中的properties类。要记住的重要一点是在读取文件或将文件写入磁盘后关闭流。否则会引起问题。以下是一个示例供您参考:
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class App
{
public static void main( String[] args )
{
Properties prop = new Properties();
try {
//load a properties file
prop.load(new FileInputStream("config.properties"));
//get the property value and print it out
System.out.println(prop.getProperty("database"));
System.out.println(prop.getProperty("dbuser"));
System.out.println(prop.getProperty("dbpassword"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
输出
localhost
mkyong
password
下面的程序使用键值对读取显示的属性文件
File f1 = new File("abcd.properties");
FileReader fin = new FileReader(f1);
Properties pr = new Properties();
pr.load(fin);
Set<String> keys = pr.stringPropertyNames();
Iterator<String> it = keys.iterator();
String key, value;
while (it.hasNext())
{
key = it.next();
value = pr.getProperty(key);
System.out.println(key+":"+value);
}
}
File f1=新文件(“abcd.properties”);
FileReader fin=新的FileReader(f1);
Properties pr=新属性();
公共荷载(fin);
设置键=pr.stringPropertyNames();
Iterator it=keys.Iterator();
字符串键,值;
while(it.hasNext())
{
key=it.next();
value=pr.getProperty(键);
System.out.println(键+“:”+值);
}
}
假设mybundle.properties文件位于类路径中,在web应用程序中读取属性文件的最大问题是您实际上不知道该文件的actaul路径。因此,我们必须使用相对路径,为此我们必须使用各种函数和类,如getresourceAsStream()、InputStream、FileinputStream等
GetReSourcesStream方法在静态和非静态方法中表现不同。。
你可以用下面的方法来做
非静态
InputStream输入=getClass().getClassLoader().getResourceAsStream(“config.properties”)代码>
静态
InputStream input=ReadPropertyFile.class.getClassLoader().getResourceAsStream(“config.properties”)代码>
如需完整参考,您可以点击以下链接。
如果您的应用程序足够小,只有少数属性来自一个或两个属性文件,那么我建议使用JDK自己的properties类,从文件加载属性,并像使用哈希表一样使用它。属性类本身从哈希表继承。但是,您的应用程序非常庞大,有相当数量的属性来自不同的来源,如属性文件、xml文件、系统属性。因此,我建议您使用Apache commons配置。它提供了来自不同配置源的属性的统一视图,并允许您为出现在不同源中的常见属性定义覆盖和首选项机制。有关使用commons配置的快速教程,请参阅本文。这可能有用:
Properties prop = new Properties();
FileReader fr = new FileReader(filename);
prop.load(fr);
Set<String> keys = pr.stringPropertyNames();
//now u can get the values from keys.
Properties prop=新属性();
FileReader fr=新的FileReader(文件名);
道具荷载(fr);
设置键=pr.stringPropertyNames();
//现在,您可以从键中获取值。
但我使用了Properties方法,但它抛出FileNotFound异常My code:Properties prop=new Properties();加载(新文件输入流(“file.properties”);但我使用了Properties方法,但它抛出FileNotFound异常我的代码:Properties prop=newproperties();加载(新文件输入流(“file.properties”);您的属性文件需要位于类路径中。但是我使用了properties方法,但它抛出FileNotFound异常我的代码:properties prop=newproperties();加载(新文件输入流(“file.properties”);在复制和粘贴答案之前,您应该先阅读信息!尽管你已经有很多书要读,我还是想推荐给你,因为它可能会帮助你对如何组织你的财产有一个基本的了解。它已经相当旧了,但是API从那时起没有太大变化
Properties prop = new Properties();
FileReader fr = new FileReader(filename);
prop.load(fr);
Set<String> keys = pr.stringPropertyNames();
//now u can get the values from keys.