java读取properties文件内容,做网站的时候,经常会把一些基本配置,写到properties文件里。如果要修改这些基本配置,就只要修改此文件就行了,不用修改java文件。
下面是读取properties文件内容的代码:
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
/**
* 读取Properties配置文件信息
* @author sunlightcs
* 2011-5-2
* http://hi.juziku.com/sunlightcs/
*/
public class PropertiesFile {
private static Logger logger = Logger.getLogger(PropertiesFile.class);
/**
* 读取Properties配置文件
* @return 返回Properties数据
*/
public Properties getPropertiesFile(){
InputStream is = this.getClass().getClassLoader().getResourceAsStream("config.properties");
Properties prop = new Properties();
try {
prop.load(is);
is.close();
} catch (IOException ex) {
logger.error("读取配置文件失败,配置文件路径为:config.properties", ex);
}
return prop;
}
/**
* 读取Properties配置文件
* @param filePath 配置文件路径
* @return 返回Properties数据
*/
public Properties getPropertiesFile(String filePath){
InputStream is = this.getClass().getClassLoader().getResourceAsStream(filePath);
Properties prop = new Properties();
try {
prop.load(is);
is.close();
} catch (IOException ex) {
logger.error("读取配置文件失败,配置文件路径为:"+filePath, ex);
}
return prop;
}
public static void main(String[] args){
Properties prop = new PropertiesFile().getPropertiesFile();
String path = prop.getProperty("imagePath");
System.out.println(path);
}
}
其中,有一个config.properties文件,放在classes目录里,文件内容如下:
#图片路径
imagePath=D:\\www\\imagePath
分享到:
相关推荐
java 读取properties文件代码
java读写properties配置文件
JAVA读取properties的值,JAVA读取properties文件,JAVA读取properties的内容
Java读取Properties文件的六种方法
java读写properties文件,解决系统找不到指定路径,解决写入后读取正常,但文件数据未更新问题
java读取properties文件的工具类,传入配置文件名字和其中的key就可以读取
java实现properties文件读取
java如何读取properties配置文件
java读写properties文件,解决系统找不到指定路径,解决写入后读取正常,但文件数据未更新问题
ConfigFile configfile = ConfigFile.getInstance("ipConfig123.properties"); String ip = configfile.getkeyvalue("ip"); 可以取出ipConfig123.properties 文件中IP的内容
利用Java的Properties 类读取配置文件信息! 读取Properties文件的例子
解决JAVA读取properties中文乱码问题
java完美读取properties文件
读取properties文件返回map
NULL 博文链接:https://liuzidong.iteye.com/blog/776637
java读取properties文件,没有用到了第三方jar包,方便实用
NULL 博文链接:https://xieke90.iteye.com/blog/2233046
怎样读取properties文件内容,用java中最基本的方式去读取properties文件中的内容