「java读配置文件」java读配置文件参数
本篇文章给大家谈谈java读配置文件,以及java读配置文件参数对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java web工程,读取配置文件路径问题
- 2、java读取配置文件的方法(xml)
- 3、Java读取配置文件的几种方法以及路径问题
- 4、如何在java类中读取Properties配置文件
- 5、java 怎么读取配置文件
- 6、linux 下java读取配置文件
java web工程,读取配置文件路径问题
读取配置文件 , xxx.properties放在webroot/WEB-INF/classes/目录下
首先将配置文件转换成InputStream,有两种方式,原理一样,都是通过类加载器得到资源:
(1)InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("xx.properties");
(2) InputStream inputStream =
this.getClass() .getClassLoader().getResourceAsStream( "xx.properties" );
调用对象的getClass()方法是获得对象当前的类类型,这部分数据存在方法区中,
而后在类类型上调用 getClassLoader()方法是得到当前类型的类加载器,我们知道在Java中所有的类都是通过加载器加载到虚拟机中的,而且类加载器之间存在父 子关系,就是子知道父,父不知道子,这样不同的子加载的类型之间是无法访问的(虽然它们都被放在方法区中),所以在这里通过当前类的加载器来加载资源也就 是保证是和类类型同一个加载器加载的。
最后调用了类加载器的getResourceAsStream()方法来加载资源。
(3) 然后加载配置文件,读取属性值
Properties prop = new Properties();
prop.load(input);
String value = prop.getProperty("PropertyName");
input.close();
java读取配置文件的方法(xml)
#include XMLDoc.hpp
——相关说明
_di_IXMLDocument 为模板类
typedef System::DelphiInterface IXMLDocument _di_IXMLDocument;
_di_IXMLNode
typedef System::DelphiInterface IXMLNode _di_IXMLNode;
_di_IXMLNodeList 同
——类方法
//设置参数
void TXXX::setOptions(String name,String value){
//创建文档对象
_di_IXMLDocument XMLDoc = LoadXMLDocument(L"文件路径");
XMLDoc-Active=true;
//文档根节点
_di_IXMLNode root = XMLDoc-DocumentElement;
//想要查找节点
_di_IXMLNode tempNode;
//调用搜索方法
searchXml(name,root,tempNode);
// 处理
if(tempNode!=NULL)
tempNode-SetText(value);
XMLDoc-SaveToFile(L"文件路径");
}
//递归搜索参数节点树
void TXXX::searchXml(String name,_di_IXMLNode Parent_Node,_di_IXMLNode tempNode){
_di_IXMLNode Child_Node; //子结点
//子节点列表
_di_IXMLNodeList list = Parent_Node-ChildNodes;
for(int i=0;ilist-Count;i++)
{
Child_Node = list-Get(i);
//递归结束条件
if(Child_Node-GetNodeName()==name)
{
tempNode = Child_Node;
break;
}
else
{
//递归函数
searchXml(name,Child_Node,tempNode);
}
}
}
Java读取配置文件的几种方法以及路径问题
.类加载器读取:
只能读取classes或者类路径中的任意资源,但是不适合读取特别大的资源。
①获取类加载器 ClassLoader cl = 类名.class.getClassLoader();
②调用类加载器对象的方法:public URL getResource(String name);
此方法查找具有给定名称的资源,资源的搜索路径是虚拟机的内置类加载器的路径。
类 URL 代表一个统一资源定位符,它是指向互联网”资源”的指针。
资源可以是简单的文件或目录,也可以是对更为复杂的对象的引用.
URL对象方法:public String getPath(),获取此 URL 的路径部分。
示例代码:
2.类加载器读取:
只能读取classes或者类路径中的任意资源,但是不适合读取特别大的资源。
①获取类加载器 ClassLoader cl = 类名.class.getClassLoader();
②调用类加载器对象的方法:public InputStream getResourceAsStream(String name);
返回读取指定资源的输入流。资源的搜索路径是虚拟机的内置类加载器的路径。
如何在java类中读取Properties配置文件
最常用读取properties文件的方法 InputStream in = getClass().getResourceAsStream("资源Name");这种方式要求properties文件和当前类在同一文件夹下面。如果在不同的包中,必须使用: InputStream ins = this.getClass().getResourceAsStream(
java 怎么读取配置文件
一.读取xml配置文件
(一)新建一个java bean(HelloBean. java)
java代码
(二)构造一个配置文件(beanConfig.xml)
xml 代码
(三)读取xml文件
1.利用ClassPathXmlApplicationContext
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.PropertiesBeanDefinitionReader来读取属性文件
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();
linux 下java读取配置文件
如果是相对路径无论windows还是linux都是一样的。
如果是绝对路径,只需要看好配置文件所在linux的目录,其他的操作和windows一样(例如:linux【/usr/local/src/1.config】,windows【c:/1.config】)
使用File就可以直接读取了。
java读配置文件的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java读配置文件参数、java读配置文件的信息别忘了在本站进行查找喔。