1.java配置文件怎么写
假设有如下xml配置文件config.xml:<?xml version="1.0" encoding="utf-8" ?>
2.java配置文件怎么写
假设有如下xml配置文件config.xml:
kiyhosinkiang100
可以用以下代码访问:
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
public class XmlConfigDemo {
public static void main(String[] args) {
try {
XMLConfiguration config = new XMLConfiguration("config.xml");
System.out.println(config.getList("name"));
System.out.println(config.getInt("info.age"));
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
}
3.怎样读取和写我自己写的一个java配置文件
import java.io.File;import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.io.SAXReader;/** * @Title: JavaBeanCreater.java * @Package liaodong.xiaoxiao.dom4j.sxd.bean * @Description: TODO(根据xml配置文件,生成javabean) * @author 辽东小小 * @date 2012-3-1 上午09:13:25 * @version V1.0 */public class JavaBeanCreater{ public static void main(String[] args) { //xml的存放路径 String xmlPath = "D:\\test\\javabean.xml"; File xmlFile = new File(xmlPath); StringBuffer sBuffer = new StringBuffer("public class "); SAXReader sReader = new SAXReader(); Document document; try { //读取xml文件,返回document对象 document = sReader.read(xmlFile); //获取根元素 Element rootElement = document.getRootElement(); // String className = rootElement.elementText("classname"); sBuffer.append(className).append("\n"); sBuffer.append("{\n"); String nature = rootElement.elementText("nature"); String natures[] = nature.split(","); //构造成员变量 for (String n : natures) { sBuffer.append("private String ").append(n).append(";\n"); } //构造set/get方法 for (String n : natures) { String methodName = n.substring(0, 1).toUpperCase()+n.substring(1); //拼写set方法 sBuffer.append("public void set").append(methodName).append("( String ").append(n).append(" )\n{"); sBuffer.append("this.").append(n).append(" = ").append(n).append(";\n}\n"); //拼写get方法 sBuffer.append("public String get").append(methodName).append(" ( )\n{\n"); sBuffer.append("return ").append(n).append(";\n}\n"); } //language 方法跟上面的类似 省略。
//写文件的方法也省略了。
sBuffer.append("}"); System.out.println(sBuffer.toString()); //}catch(Exception e){ e.printStackTrace(); } }}用Dom4j解析的xml##############################################################运行结果为:public class animal{private String name;private String food;private String age;public void setName( String name ){this.name = name;}public String getName ( ){return name;}public void setFood( String food ){this.food = food;}public String getFood ( ){return food;}public void setAge( String age ){this.age = age;}public String getAge ( ){return age;}}。
4.怎样读取和写我自己写的一个java配置文件
import java.io.File;import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.io.SAXReader;/** * @Title: JavaBeanCreater.java * @Package liaodong.xiaoxiao.dom4j.sxd.bean * @Description: TODO(根据xml配置文件,生成javabean) * @author 辽东小小 * @date 2012-3-1 上午09:13:25 * @version V1.0 */public class JavaBeanCreater{ public static void main(String[] args) { //xml的存放路径 String xmlPath = "D:\\test\\javabean.xml"; File xmlFile = new File(xmlPath); StringBuffer sBuffer = new StringBuffer("public class "); SAXReader sReader = new SAXReader(); Document document; try { //读取xml文件,返回document对象 document = sReader.read(xmlFile); //获取根元素 Element rootElement = document.getRootElement(); // String className = rootElement.elementText("classname"); sBuffer.append(className).append("\n"); sBuffer.append("{\n"); String nature = rootElement.elementText("nature"); String natures[] = nature.split(","); //构造成员变量 for (String n : natures) { sBuffer.append("private String ").append(n).append(";\n"); } //构造set/get方法 for (String n : natures) { String methodName = n.substring(0, 1).toUpperCase()+n.substring(1); //拼写set方法 sBuffer.append("public void set").append(methodName).append("( String ").append(n).append(" )\n{"); sBuffer.append("this.").append(n).append(" = ").append(n).append(";\n}\n"); //拼写get方法 sBuffer.append("public String get").append(methodName).append(" ( )\n{\n"); sBuffer.append("return ").append(n).append(";\n}\n"); } //language 方法跟上面的类似 省略。
//写文件的方法也省略了。
sBuffer.append("}"); System.out.println(sBuffer.toString()); //}catch(Exception e){ e.printStackTrace(); } }}用Dom4j解析的xml##############################################################运行结果为:public class animal{private String name;private String food;private String age;public void setName( String name ){this.name = name;}public String getName ( ){return name;}public void setFood( String food ){this.food = food;}public String getFood ( ){return food;}public void setAge( String age ){this.age = age;}public String getAge ( ){return age;}}。
5.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();
6.Java中的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();
}
}
7.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 = "";
转载请注明出处育才学习网 » java怎么写配置文件