「java如何创建配置文件」java写配置文件
今天给各位分享java如何创建配置文件的知识,其中也会对java写配置文件进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java怎么在包中创建配置文件
- 2、javaweb怎样在classpath建立一个property配置文件
- 3、如何自定义一套java后台项目配置文件xml,不是j2ee那种
- 4、java配置文件怎么写?
java怎么在包中创建配置文件
先看代码目录结构:
src/weather/
QueryWeather.java
weather.xml
程序里面可以直接读取到weather.xml文件,代码如下:
private static String getXmlContent()throws IOException {
FileReader f = new FileReader("src/weather/weather.xml");
BufferedReader fb = new BufferedReader(f);
StringBuffer sb = new StringBuffer("");
String s = "";
while((s = fb.readLine()) != null) {
sb = sb.append(s);}return sb.toString();}但是一旦把这个class文件和xml文件打成jar包再运行,对不起,报错,QueryWeather.class字节码根本找不到weather.xml
在看打成jar包的结构:META-INFMANIFEST.MFweatherQueryWeather.class
weather.xml
用下面的方法就可以找到weather.xml
private static String getXmlContent()throws IOException {
Reader f = new InputStreamReader(QueryWeather.class.getClass().getResourceAsStream("/weather/weather.xml"));
BufferedReader fb = new BufferedReader(f);
StringBuffer sb = new StringBuffer("");
String s = "";
javaweb怎样在classpath建立一个property配置文件
java action读取src目录下的properties配置文件。
mailServer.properties配置文件如下:
mailServerHost = smtp.163.com
mailServerPort = 25
authValidate = true
userName = test@163.com
读取配置文件类GetProperty代码如下:
package com.hsinghsu.test.action;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class GetProperty {
// 方法一:通过java.util.ResourceBundle读取资源属性文件
public static String getPropertyByName(String path, String name) {
String result = "";
try {
// 方法一:通过java.util.ResourceBundle读取资源属性文件
result = java.util.ResourceBundle.getBundle(path).getString(name);
System.out.println("name:" + result);
} catch (Exception e) {
System.out.println("getPropertyByName2 error:" + name);
}
return result;
}
// 方法二:通过类加载目录getClassLoader()加载属性文件
public static String getPropertyByName2(String path, String name) {
String result = "";
// 方法二:通过类加载目录getClassLoader()加载属性文件
InputStream in = GetProperty.class.getClassLoader()
.getResourceAsStream(path);
// InputStream in =
// this.getClass().getClassLoader().getResourceAsStream("mailServer.properties");
// 注:Object.class.getResourceAsStream在action中调用报错,在普通java工程中可用
// InputStream in =
// Object.class.getResourceAsStream("/mailServer.properties");
Properties prop = new Properties();
try {
prop.load(in);
result = prop.getProperty(name).trim();
System.out.println("name:" + result);
} catch (IOException e) {
System.out.println("读取配置文件出错");
e.printStackTrace();
}
return result;
}
}
如何自定义一套java后台项目配置文件xml,不是j2ee那种
在resources里面新建一个XML file或者 file,
XML file 会自动生成XML头,在下面加入内容就可以了,首先要有一个根节点,然后如果需要用到一些类,如:spring的一些类,就需要引入包,如:
?xml version="1.0" encoding="UTF-8"?
beans xmlns=""
xmlns:mvc=""
xsi:schemaLocation="
"
context:component-scan base-package="com.dist.*.controller" /
/beans
其中?xml ……就是头,beans 是根节点,下面的content:……是内容。
如果添加的事properties文件,格式如下:
# 连接池配置
pool.size = 2
pool.max = 50
java配置文件怎么写?
参考java.util.Properties对象进行书写,另外可以在网上找一写辅助书写材料。
代码:
public static void main(String[] args) {
Properties p = new Properties();
p.setProperty("id", "user1");
p.setProperty("password", "123456");
try{
PrintStream stm = new PrintStream(new File("e:\test.properties"));
p.list(stm);
} catch (IOException e) {
e.printStackTrace();
}
}
java如何创建配置文件的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java写配置文件、java如何创建配置文件的信息别忘了在本站进行查找喔。
发布于:2022-12-20,除非注明,否则均为
原创文章,转载请注明出处。