properties读取java
1. 请教java如何实时读取.properties文件中的配置项内容
Properties 类
static {
properties = new Properties();
InputStream in = null;
try {
in = ReadProperties.class.getResourceAsStream("/brochina-config.properties");//文件路径
properties.load(in);
} catch (Exception e) {
e.printStackTrace();
}
finally{
try {
if(in!=null) in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
String value = properties.getProperty(key);具体方法
2. java中怎么读取properties文件
(1)load(InputStream inStream)
这个方法可以从.properties属性文件对应的文件输入流中,加载属性列表到Properties类对象。如下面的代码:
Properties pro = new Properties();
FileInputStream in = new FileInputStream("a.properties");
pro.load(in);
in.close();
(2)store(OutputStream out, String comments)
这个方法将Properties类对象的属性列表保存到输出流中。如下面的代码:
FileOutputStream oFile = new FileOutputStream(file, "a.properties");
pro.store(oFile, "Comment");
oFile.close();
3. java 怎么读取配置文件
一.读取xml配置文件
(一)新建一个java bean(HelloBean. java)
java代码
(二)构造一个配置文件(beanConfig.xml)
xml 代码
(三)读取xml文件
1.利用
java代码
2.利用FileSystemResource读取
java代码
二.读取properties配置文件
这里介绍两种技术:利用spring读取properties 文件和利用java.util.Properties读取
(一)利用spring读取properties 文件
我们还利用上面的HelloBean. java文件,构造如下beanConfig.properties文件:
properties 代码
helloBean.class=chb.demo.vo.HelloBean
helloBean.helloWorld=Hello!chb!
属性文件中的"helloBean"名称即是Bean的别名设定,.class用于指定类来源。
然后利用org.springframework.beans.factory.support.来读取属性文件
java代码
(二)利用java.util.Properties读取属性文件
比如,我们构造一个ipConfig.properties来保存服务器ip地址和端口,如:
properties 代码
ip=192.168.0.1
port=8080
三.读取位于Jar包之外的properties配置文件
下面仅仅是列出读取文件的过程,剩下的解析成为properties的方法同上
1 FileInputStream reader = new FileInputStream("config.properties");
2 num = reader.read(byteStream);
3 ByteArrayInputStream inStream = new ByteArrayInputStream(byteStream, 0, num);
四.要读取的配置文件和类文件一起打包到一个Jar中
String currentJarPath = URLDecoder.decode(YourClassName.class.getProtectionDomain().getCodeSource().getLocation().getFile(), "UTF-8"); //获取当前Jar文件名,并对其解码,防止出现中文乱码
JarFile currentJar = new JarFile(currentJarPath);
JarEntry dbEntry = currentJar.getJarEntry("包名/配置文件");
InputStream in = currentJar.getInputStream(dbEntry);
//以上YourClassName是class全名,也就是包括包名
修改:
JarOutputStream out = new FileOutputStream(currentJarPath);
out.putNextEntry(dbEntry);
out.write(byte[] b, int off, int len); //写配置文件
。。。
out.close();
4. java怎么获取properties文件中的值
按照读取属性文件的方法即可,如下:
5. JAVA中如何读取src下所有的properties文件
1.使用java.util.Properties类的load()方法
示例:
//文件在项目下。不是在包下!!
InputStream in = new BufferedInputStream(new FileInputStream("demo.properties")) ;
Properties p = new Properties();
p.load(in) ;
String className2 = p.getProperty("database.driver");
String url = p.getProperty("database.url");
String user = p.getProperty("database.user");
String password = p.getProperty("database.pass");
总结:如果是 在WEB上读取properties文件,写成下面这种。上面写的那些只在 JavaSE 中
String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
System.out.println(path);
InputStream in = new FileInputStream(new File(path+File.separator+"mysql.properties"));
Properties prop = new Properties();
6. java读取properties配置文件路径问题
说说我的项目中的情况吧:
配置文件“weblogic11g.properties”保存在WEB-INFO目录下,和web.xml在同一个目录下。
一个JavaBean专门用于读取配置文件的内容:
public class PropertiesIO {
private String fileName = null;
public PropertiesIO(String fileName){
this.fileName = getClass().getClassLoader().getResource("/").getPath() + "..\\" + fileName;
}
public String getValue(String key){
try{
InputStream in = new FileInputStream(fileName);
Properties prop = new Properties();
prop.load(in);
in.close();
return prop.getProperty(key);
}
catch(Exception err){
err.printStackTrace();
return null;
}
}
}
重点说明:getClass().getClassLoader().getResource("/")会得到当前项目下的“WEB-INF\classes”目录,即JavaBean的*.class文件的根目录,
getClass().getClassLoader().getResource("/").getPath() + "..\\" + fileName
就会得到当前项目下的“WEB-INF\weblogic11g.properties”文件。
getValue()是根据键值得到相应配置项的内容,这样就简单了。
7. 在java中如何读取properties文件
最常用读取properties文件的方法
InputStream in = getClass().getResourceAsStream("资源Name");这种方式要求properties文件和当前类在同一文件夹下面。如果在不同的包中,必须使用:
InputStream ins = this.getClass().getResourceAsStream("/cn/zhao/properties/testPropertiesPath2.properties");
Java中获取路径方法
获取路径的一个简单实现
反射方式获取properties文件的三种方式
1 反射方式获取properties文件最常用方法以及思考:
Java读取properties文件的方法比较多,网上最多的文章是"Java读取properties文件的六种方法",但在Java应用中,最常用还是通过java.lang.Class类的getResourceAsStream(String name) 方法来实现,但我见到众多读取properties文件的代码中,都会这么干:
InputStream in = getClass().getResourceAsStream("资源Name");
这里面有个问题,就是getClass()调用的时候默认省略了this!我们都知道,this是不能在static(静态)方法或者static块中使用的,原因是static类型的方法或者代码块是属于类本身的,不属于某个对象,而this本身就代表当前对象,而静态方法或者块调用的时候是不用初始化对象的。
问题是:假如我不想让某个类有对象,那么我会将此类的默认构造方法设为私有,当然也不会写别的共有的构造方法。并且我这个类是工具类,都是静态的方法和变量,我要在静态块或者静态方法中获取properties文件,这个方法就行不通了。
那怎么办呢?其实这个类就不是这么用的,他仅仅是需要获取一个Class对象就可以了,那还不容易啊--
取所有类的父类Object,用Object.class难道不比你的用你正在写类自身方便安全吗 ?呵呵,下面给出一个例子,以方便交流。
import java.util.Properties;
import java.io.InputStream;
import java.io.IOException;
/**
* 读取Properties文件的例子
* File: TestProperties.java
* User: leimin
* Date: 2008-2-15 18:38:40
*/
public final class TestProperties {
private static String param1;
private static String param2;
static {
Properties prop = new Properties();
InputStream in = Object. class .getResourceAsStream( "/test.properties" );
try {
prop.load(in);
param1 = prop.getProperty( "initYears1" ).trim();
param2 = prop.getProperty( "initYears2" ).trim();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 私有构造方法,不需要创建对象
*/
private TestProperties() {
}
public static String getParam1() {
return param1;
}
public static String getParam2() {
return param2;
}
public static void main(String args[]){
System.out.println(getParam1());
System.out.println(getParam2());
}
}
运行结果:
151
152
当然,把Object.class换成int.class照样行,呵呵,大家可以试试。
另外,如果是static方法或块中读取Properties文件,还有一种最保险的方法,就是这个类的本身名字来直接获取Class对象,比如本例中可写成TestProperties.class,这样做是最保险的方法
2 获取路径的方式:
File fileB = new File( this .getClass().getResource( "" ).getPath());
System. out .println( "fileB path: " + fileB);
2.2获取当前类所在的工程名:
System. out .println("user.dir path: " + System. getProperty ("user.dir"))<span style="background-color: white;">3 获取路径的一个简单的Java实现</span>
/**
*获取项目的相对路径下文件的绝对路径
*
* @param parentDir
*目标文件的父目录,例如说,工程的目录下,有lib与bin和conf目录,那么程序运行于lib or
* bin,那么需要的配置文件却是conf里面,则需要找到该配置文件的绝对路径
* @param fileName
*文件名
* @return一个绝对路径
*/
public static String getPath(String parentDir, String fileName) {
String path = null;
String userdir = System.getProperty("user.dir");
String userdirName = new File(userdir).getName();
if (userdirName.equalsIgnoreCase("lib")
|| userdirName.equalsIgnoreCase("bin")) {
File newf = new File(userdir);
File newp = new File(newf.getParent());
if (fileName.trim().equals("")) {
path = newp.getPath() + File.separator + parentDir;
} else {
path = newp.getPath() + File.separator + parentDir
+ File.separator + fileName;
}
} else {
if (fileName.trim().equals("")) {
path = userdir + File.separator + parentDir;
} else {
path = userdir + File.separator + parentDir + File.separator
+ fileName;
}
}
return path;
}
4 利用反射的方式获取路径:
InputStream ips1 = Enumeration . class .getClassLoader() .getResourceAsStream( "cn/zhao/enumStudy/testPropertiesPath1.properties" );
InputStream ips2 = Enumeration . class .getResourceAsStream( "testPropertiesPath1.properties" );
InputStream ips3 = Enumeration . class .getResourceAsStream( "properties/testPropertiesPath2.properties" );