java读取properties文件

java读取properties文件,第1张

个人建议使用FileInputStrean流 你试试看。因为文件流加载文件应该没问题。

InputStream in = getPropertiesclassgetClassLoad()getResourceAsStream(

"configproperties");这句改成FileInputStream in=new FileInputStream("/configproperties")看看。

一读取xml配置文件

(一)新建一个java bean(HelloBean java)

java代码

(二)构造一个配置文件(beanConfigxml)

xml 代码

(三)读取xml文件

1利用ClassPathXmlApplicationContext

java代码

2利用FileSystemResource读取

java代码

二读取properties配置文件

这里介绍两种技术:利用spring读取properties 文件和利用javautilProperties读取

(一)利用spring读取properties 文件

我们还利用上面的HelloBean java文件,构造如下beanConfigproperties文件:

properties 代码

helloBeanclass=chbdemovoHelloBean

helloBeanhelloWorld=Hello!chb!

属性文件中的"helloBean"名称即是Bean的别名设定,class用于指定类来源。

然后利用orgspringframeworkbeansfactorysupportPropertiesBeanDefinitionReader来读取属性文件

java代码

(二)利用javautilProperties读取属性文件

比如,我们构造一个ipConfigproperties来保存服务器ip地址和端口,如:

properties 代码

ip=19216801

port=8080

三读取位于Jar包之外的properties配置文件

下面仅仅是列出读取文件的过程,剩下的解析成为properties的方法同上

1 FileInputStream reader = new FileInputStream("configproperties");

2 num = readerread(byteStream);

3 ByteArrayInputStream inStream = new ByteArrayInputStream(byteStream, 0, num);

四要读取的配置文件和类文件一起打包到一个Jar中

String currentJarPath = URLDecoderdecode(YourClassNameclassgetProtectionDomain()getCodeSource()getLocation()getFile(), "UTF-8"); //获取当前Jar文件名,并对其解码,防止出现中文乱码

JarFile currentJar = new JarFile(currentJarPath);

JarEntry dbEntry = currentJargetJarEntry("包名/配置文件");

InputStream in = currentJargetInputStream(dbEntry);

//以上YourClassName是class全名,也就是包括包名

修改:

JarOutputStream out = new FileOutputStream(currentJarPath);

outputNextEntry(dbEntry);

outwrite(byte[] b, int off, int len); //写配置文件

。。。

outclose();

myeclipse中propload报错是因为没有找到要加载的properties文件。

Java可使用Properties类读写properties,具体说明如下:

1Properties类与Properties配置文件

Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集。不过Properties有特殊的地方,就是它的键和值都是字符串类型。

2Properties中的主要方法

(1)load(InputStream inStream)

这个方法可以从properties属性文件对应的文件输入流中,加载属性列表到Properties类对象。如下面的代码:

Properties pro = new Properties();

FileInputStream in = new FileInputStream("aproperties");

proload(in);

inclose();

(2)store(OutputStream out, String comments)

这个方法将Properties类对象的属性列表保存到输出流中。如下面的代码:

FileOutputStream oFile = new FileOutputStream(file, "aproperties");

prostore(oFile, "Comment");

oFileclose();

如果comments不为空,保存后的属性文件第一行会是#comments,表示注释信息;如果为空则没有注释信息。

注释信息后面是属性文件的当前保存时间信息。

(3)getProperty/setProperty

这两个方法是分别是获取和设置属性信息。

3代码实例

属性文件aproperties如下:

name=root

pass=liu

key=value

读取aproperties属性列表,与生成属性文件bproperties。代码如下:

import javaioBufferedInputStream;

import javaioFileInputStream;

import javaioFileOutputStream;

import javaioInputStream;

import javautilIterator;

import javautilProperties;

public class PropertyTest {

public static void main(String[] args) {

try {

// 读取属性文件aproperties

InputStream in = new BufferedInputStream(new FileInputStream("aproperties"));

// /加载属性列表

Properties prop = new Properties();

propload(in);

Iterator<String> it = propstringPropertyNames()iterator();

while (ithasNext()) {

String key = itnext();

Systemoutprintln(key + ":" + propgetProperty(key));

}

inclose();

// /保存属性到bproperties文件

FileOutputStream oFile = new FileOutputStream("bproperties", true);// true表示追加打开

propsetProperty("phone", "10086");

propstore(oFile, "The New properties file");

oFileclose();

} catch (Exception e) {

Systemoutprintln(e);

}

}

}

方式一:采用ServletContext读取,读取配置文件的realpath,然后通过文件流读取出来。因为是用ServletContext读取文件路径,所以配置文件可以放入在web-info的classes目录中,也可以在应用层级及web-info的目录中。文件存放位置具体在eclipse工程中的表现是:可以放在src下面,也可放在web-info及webroot下面等。因为是读取出路径后,用文件流进行读取的,所以可以读取任意的配置文件包括xml和properties。缺点:不能在servlet外面应用读取配置信息。

方式二:采用ResourceBundle类读取配置信息,

优点是:可以以完全限定类名的方式加载资源后,直接的读取出来,且可以在非Web应用中读取资源文件。缺点:只能加载类classes下面的资源文件且只能读取properties文件。

方式三:采用ClassLoader方式进行读取配置信息

优点是:可以在非Web应用中读取配置资源信息,可以读取任意的资源文件信息

缺点:只能加载类classes下面的资源文件。

方法4 getResouceAsStream

XmlParserHandlerclassgetResourceAsStream 与classloader不同

以上就是关于java读取properties文件全部的内容,包括:java读取properties文件、java 怎么读取配置文件、用Servlet读取Properties文件为什么Properties.load方法报错等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9673571.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存