「java解析prop」java解析xml的几种方式

博主:adminadmin 2022-12-16 20:09:06 60

今天给各位分享java解析prop的知识,其中也会对java解析xml的几种方式进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

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()是根据键值得到相应配置项的内容,这样就简单了。

在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: leizhimin

* 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" );

java如何读取.properties文件的数据

在prop包下建立LoadProp.java文件。 3.有很多方法来读取.properties文件,现将主要方法罗列出来: a.通过class的getResourceAsStream()方法来读取 package prop; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class LoadProp { public static void main(String[] args) { LoadProp loadProp = new LoadProp(); InputStream in = loadProp.getClass().getResourceAsStream("/config/a.properties"); Properties prop = new Properties(); try { prop.load(in); } catch (IOException e) { e.printStackTrace(); } System.out.println(prop.getProperty("name", "none")); System.out.println(prop.getProperty("age", "none")); } } 一定要注意的是,class里的getResourceAsStream()方法里参数的类路径一定要在前面加上"/",否则会报错 b.使用class的getClassLoader()方法所得的ClassLoader的getResourceAsStream()来读取 package prop; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class LoadProp { public static void main(String[] args) { LoadProp loadProp = new LoadProp(); InputStream in = loadProp.getClass().getClassLoader().getResourceAsStream("config/a.properties"); Properties prop = new Properties(); try { prop.load(in); } catch (IOException e) { e.printStackTrace(); } System.out.println(prop.getProperty("name", "none")); System.out.println(prop.getProperty("age", "none")); } } ClassLoader的getResourceAsStream()方法与Class的getResourceAsStream()方法有点区别,在这里一定不要在类路径前面加上"/",否则会报错,是不是很奇怪。 c.使用ResourceBundle来读取 package prop; import java.util.ResourceBundle; public class LoadProp { public static void main(String[] args) { ResourceBundle rb = ResourceBundle.getBundle("config/a"); System.out.println(rb.getString("name")); System.out.println(rb.getString("age")); } } 注意,getBundle()方法里的参数,是baseName,不要把后缀名写出来,并且不要加"/"。 好了,这是读取.properties文件的几种主要方法,还有其他的方法,基本上都大同小异。

关于java解析prop和java解析xml的几种方式的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-16,除非注明,否则均为首码项目网原创文章,转载请注明出处。