java怎么写配置文件
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 = "";
struts2配置文件怎么写
1.简述Struts2的配置文件包括哪些元素
1、package元素
package用来管理action:使用了面向对象的思想
属性:
name:用来定义包名,注意:同一个配置文件中包名是唯一的。
extends:指定所继承的包,一般情况下需要继承struts-default包
truts-default.xml中定义着struts-default这个包。
truts-default.xml是在我们的struts.xml加载之前加载。
abstract:指定包是抽象的,只能被继承,含有action的包不能是抽象的,默认值是false。
namespace:名称空间
名称空间 + 动作名称 = 访问路径
<package name="p1" extends="struts-default" namespace="/a/b"></package>
1
struts2查找动作的方法:
1:先按照用户输入的路径查找路径: /a/b/c/d/action1.action
如果没有找到再向上一层路径找:/a/b/c 找到了(不会再继续向上找)该路径则执行第二步
再向上一层路径找:/a/b 找到了(不会再继续向上找)该路径则执行第二步,找不到继续向上一级找
再向上一层路径找:/a 没找到 找到了(不会再继续向上找)该路径则执行第二步
再找/ 也没有的话去默认命名空间下找。
都没找到直接报错
2:找action: 先在当前的命名空间下找 ,如果没有找到则取默认的命名空间(没有写namespace属性的)下找 ,如果还找不到报错
一般只要我们有默认命名空间的pakege标签时,第一步是不会报错的。
2.请帮我详细解释Struts2的配置文件
<constant name="struts.configuration.xml.reload" value="true"></constant>;//是说,当你的配置文件改变是,struts自动读取,不需要你重启服务。
<constant name="struts.objectFactory" value="spring"></constant>;//与spring框架结合使用,你的action对象由spring帮助生成了。
<package name="" extends="struts-default" namespace="/">;//这个struts的包名,继承struts-default,就是这个写,你也不需要多想,name写什么都行
//UsersAction这个类实现了action接口,给这个action起来个名字叫usersAction_*,*的意思是说,usersAction_后面可以跟要执行的方法名,这与method="{1}"相呼应,*的名字,就是{1}的方法名
<action name="usersAction_*" class="UsersAction" method="{1}">
<result name="success">/result.jsp</result>//return “SUCCESS”时的转向路径,注意我的大小写
<result name="input">/login.jsp</result>>//return “INPUT”时的转向路径,注意我的大小写
<result name="list">/list.jsp</result>>//return “list”时的转向路径,注意我的大小写
</action>
</package>
3.struts2 的struts.xml文件怎么配置
struts2中组件化的方式 可以将每个功能模块独立到一个xml配置文件中 然后用include节点引用 -->
转载请注明出处育才学习网 » 数据库配置文件怎么写
育才学习网