导航:首页 > 文件教程 > java通用读取配置文件

java通用读取配置文件

发布时间:2023-03-03 15:39:32

java读取文件的几种方式

方式一:采用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
XmlParserHandler.class.getResourceAsStream 与classloader不同

linux 下java读取配置文件

linux下也是文件系统,同样的也可以使用file对象来读取配置文件信息,示例如下:
import java.io.*;

public class FileToString {
public static String readFile(String fileName) {
String output = "";

File file = new File(fileName);//建立file对象

if(file.exists()){//判断是否存在
if(file.isFile()){//判断是否文件
try{
BufferedReader input = new BufferedReader (new FileReader(file));
StringBuffer buffer = new StringBuffer();
String text;

while((text = input.readLine()) != null)
buffer.append(text +"/n");//读取内容进行拼接。

output = buffer.toString();
}
catch(IOException ioException){
System.err.println("File Error!");

}
}
else if(file.isDirectory()){//是否为文件夹
String[] dir = file.list();
output += "Directory contents:/n";

for(int i=0; i<dir.length; i++){
output += dir[i] +"/n";
}
}
}
else{
System.err.println("Does not exist!");
}
return output;
}
}

Ⅲ java 怎么读取web jar中的某个配置文件

项目迁移的过程中发现以前的代码维护性实在是差。
我把问题简化为以下这专些简单的代码:属
项目M
引用了项目
A.jar,这个A在lib目录里面
在A里面放置了一个配置文件test.properties,
就放在jar的根目录下。
A.jar
|___test.properties
在M中有一段代码回去读取这个A.jar里的配置文件,简单一点就用下面这句话来调用。
Java
code
public
class
ConfigUtil
{
public
static
String
getInstance()
throws
Exception{
String
path
=
ConfigUtil.class.getResource("/").toString();
path
=
path.substring(0,
path.length()-8);//
System.out.println(path);//这里打印的结果显示可以拿到当前类的绝对路径
InputStream
f
=
new
FileInputStream("jar:"+path+"lib!/A.jar/"+"test.properties");
return
"xxx";
}
}

Ⅳ Java中如何设置读取ini配置文件

/*
* IniReader.java
* 用Java读取INI文件(带section的)
* 示例:
* IniReader reader = new IniReader("E:\\james\\win.ini");
* out.println(reader.getValue("TestSect3", "kkk 6"));
*/

package tmp;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;

public class IniReader {

protected HashMap sections = new HashMap();
private transient String currentSecion;
private transient Properties current;

public IniReader(String filename) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(filename));
read(reader);
reader.close();
}

protected void read(BufferedReader reader) throws IOException {
String line;
while ((line = reader.readLine()) != null) {
parseLine(line);
}
}

protected void parseLine(String line) {
line = line.trim();
if (line.matches("\\[.*\\]")) {
currentSecion = line.replaceFirst("\\[(.*)\\]", "$1");
current = new Properties();
sections.put(currentSecion, current);
} else if (line.matches(".*=.*")) {
if (current != null) {
int i = line.indexOf('=');
String name = line.substring(0, i);
String value = line.substring(i + 1);
current.setProperty(name, value);
}
}
}

public String getValue(String section, String name) {
Properties p = (Properties) sections.get(section);

if (p == null) {
return null;
}

String value = p.getProperty(name);
return value;
}

}

ini文件如下:

[TestSect1]
kkk 1=VVVVVVVVVVV1
kkk 2=VVVVVVVVVVV2

[TestSect2]
kkk 3=VVVVVVVVVVV3
kkk 4=VVVVVVVVVVV4

[TestSect3]
kkk 5=VVVVVVVVVVV5
kkk 6=VVVVVVVVVVV6

Ⅳ 如何在java类中读取Properties配置文件

最常用读取properties文件的方法 InputStream in = getClass().getResourceAsStream("资源Name");这种方式要求properties文件和当前类在同一文件夹下面。如果在不同的包中,必须使用: InputStream ins = this.getClass().getResourceAsStream(

Ⅵ 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;i<list->Count;i++)
{
Child_Node = list->Get(i);
//递归结束条件
if(Child_Node->GetNodeName()==name)
{
tempNode = Child_Node;
break;
}
else
{
//递归函数
searchXml(name,Child_Node,tempNode);
}
}
}

Ⅶ java web工程,读取配置文件路径问题

读取配置文件 , xxx.properties放在/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怎样提取配置文件!怎么才能采用ServletContext读取

创建配置文件:
1、在项目的任意地方,右键-》New-》File-》FileName-》输入-》名称.properties(比如:config.properties)
2、访问路径:从根目录开始出发(WebRoot)->WEB-INF->classes->config.properties,(如果有包名,在classes->包名->config.properties)(路径可以直接从本地中项目的路径,找到WEB-INF直接从地址中(比如我的本地磁盘保存是这样的:F:\课程\s2课程\s2书上内容\Java Web\ServletTest\WebRoot\WEB-INF\classes\config.properties))

response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
/************************使用servletContext.getResourceAsStream**************************************/
//实例化ServletContext
ServletContext servletContext=this.getServletContext();
// //获取输入流
// InputStream in=servletContext.getResourceAsStream("\\WEB-INF\\classes\\config.properties");
// Properties p=new Properties();
// //类的装载
// p.load(in);
// //拿到配置文件中userName参数
// out.println(p.getProperty("userName"));

/***************************普通的获取配置文件**************************************/
String path= servletContext.getRealPath(("\\WEB-INF\\classes\\config.properties"));//拿到绝对路径
FileInputStream in=new FileInputStream(path);
Properties p=new Properties();
p.load(in);
out.println(p.get("userName"));

Ⅸ Java读取xml配置文件:

你用DOM把账号节点取出来
然后用String中的split方法做切割
就可以放到数组里了
如果单纯是想看账号是不是在这个里面
你也可以直接String.indexof查找读出来的信息中有没有你要的账号

阅读全文

与java通用读取配置文件相关的资料

热点内容
maya粒子表达式教程 浏览:84
抖音小视频如何挂app 浏览:283
cad怎么设置替补文件 浏览:790
win10启动文件是空的 浏览:397
jk网站有哪些 浏览:134
学编程和3d哪个更好 浏览:932
win10移动硬盘文件无法打开 浏览:385
文件名是乱码还删不掉 浏览:643
苹果键盘怎么打开任务管理器 浏览:437
手机桌面文件名字大全 浏览:334
tplink默认无线密码是多少 浏览:33
ipaddgm文件 浏览:99
lua语言编程用哪个平台 浏览:272
政采云如何导出pdf投标文件 浏览:529
php获取postjson数据 浏览:551
javatimetask 浏览:16
编程的话要什么证件 浏览:94
钱脉通微信多开 浏览:878
中学生学编程哪个培训机构好 浏览:852
荣耀路由TV设置文件共享错误 浏览:525

友情链接