java配置文件怎么写

1.java配置文件怎么写

假设有如下xml配置文件config.xml:

kiyhosinkiang100

可以用以下代码访问:

import org.apache.commons.configuration.;

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 ( e) {

e.printStackTrace();

}

}

}

2.java配置文件怎么写

假设有如下xml配置文件config.xml:<?xml version="1.0" encoding="utf-8" ?>kiyhosinkiang100可以用以下代码访问:import org.apache.commons.configuration.;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 ( 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 中怎么创建

1.一般在scr下面新建一个属性文件*.properties,如a.properties 然后在Java程序中读取或操作这个属性文件。

代码实例 属性文件a.properties如下:name=root pass=liu key=value 读取a.properties属性列表,与生成属性文件b.properties。代码如下:1 import java.io.BufferedInputStream; 2 import java.io.FileInputStream; 3 import java.io.FileOutputStream; 4 import java.io.InputStream; 5 import java.util.Iterator; 6 import java.util.Properties; 7 8 public class PropertyTest { 9 public static void main(String[] args) { 10 Properties prop = new Properties(); 11 try{12 //读取属性文件a.properties13 InputStream in = new BufferedInputStream (new FileInputStream("a.properties"));14 prop.load(in); ///加载属性列表15 Iterator it=prop.stringPropertyNames().iterator();16 while(it.hasNext()){17 String key=it.next();18 System.out.println(key+":"+prop.getProperty(key));19 }20 in.close();21 22 ///保存属性到b.properties文件23 FileOutputStream oFile = new FileOutputStream("b.properties", true);//true表示追加打开24 prop.setProperty("phone", "10086");25 prop.store(oFile, "The New properties file");26 oFile.close();27 }28 catch(Exception e){29 System.out.println(e);30 }31 } 32 } getProperty/setProperty这两个方法是分别是获取和设置属性信息。

Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集。不过Properties有特殊的地方,就是它的键和值都是字符串类型。

*.properties文件的注释用#。配置数据的时候是以键值对的形式,调用的时候和修改的时候也是操作键值对。

2.当然还可以用*.xml来配置,位置一般在一个包下面。例如com.styspace包下面的config.properties文件。

xml version="1.0" encoding="gbk"?> 100001 123 李四 1000000.00 现在操作config.properties文件。import org.apache.commons.configuration.Configuration; import org.apache.commons.configuration.; import org.apache.commons.configuration.; public class peropertiesLoaderTest { public static void main(String[] args) throws { Configuration config = new ("com/styspace/config.properties"); String name = config.getString("name"); System.out.println("name:" + name); } }。

5.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();

}

}

6.java 怎么读取配置文件

一.读取xml配置文件

(一)新建一个java bean(HelloBean. java)

java代码

(二)构造一个配置文件(beanConfig.xml)

xml 代码

(三)读取xml文件

1.利用

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.来读取属性文件

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();

7.怎样读取和写我自己写的一个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;}}。

java配置文件怎么写

转载请注明出处育才学习网 » java配置文件怎么写

知识

石字甲骨文怎么写

阅读(660)

本文主要为您介绍石字甲骨文怎么写,内容包括甲骨文的石字怎么写,''石‘’字的甲骨文怎么写,石古代字怎么写甲骨文。

知识

昊用小篆字怎么写

阅读(311)

本文主要为您介绍昊用小篆字怎么写,内容包括昊字篆书怎么写法,昊的篆体怎么写,昊字篆书怎么写昊字篆书怎么写法。姚心雨用小篆体,如下:

知识

授权书日期怎么写

阅读(285)

本文主要为您介绍授权书日期怎么写,内容包括授权书怎么写,授权委托书怎么写期限是多长时间,法定代表人授权委托书委托期限怎么写。授权书分为厂家给代理商的授权,或者公司给个人的授权,使用场合大多在招投标中,当然也会有其他的场合,不过格式上

知识

careergoal怎么写

阅读(219)

本文主要为您介绍careergoal怎么写,内容包括CareerGoal怎么写,careergoal该怎么写,master读完想读phd,那么申请master时careergoal要怎么写呢。其实如果你是转专业读PHD,读个MASTER非常有用,而且非常好说。如果不是转专业,你可以说你想先熟悉

知识

向上级的责任状怎么写

阅读(220)

本文主要为您介绍向上级的责任状怎么写,内容包括责任书怎么写,管理之类的责任书怎么写啊,如何写责任状。原发布者:会计师菜鸟ON安全责任书怎么写安全责任书怎么写责任书是在自己的岗位上分内应做的事,没有做好自己工作时应承担的不利后果的文

知识

岳飞传观后感怎么写

阅读(226)

本文主要为您介绍岳飞传观后感怎么写,内容包括岳飞传读后感怎么写,《岳飞传》读后感大致怎么写,岳飞传读后感怎么写。袁腾飞讲述的《两宋风云》可谓讲解颇细、建树颇丰。但是,人们在听取那些“忠臣”、“奸臣”、“昏君”“明君”的过程中,在

知识

勘察方案怎么写

阅读(220)

本文主要为您介绍勘察方案怎么写,内容包括现场勘察情况怎么写,勘察和方案设计文件包括哪些,设计方案怎么写。现场勘察安全防范工程设计前,应进行现场勘察。 现场勘察内容和要求:1.1系统全面的调查和了解被防护对象本身的基本情况。1) 被防护对

知识

电视综艺节目的未来发展趋势的摘要怎么写

阅读(256)

本文主要为您介绍电视综艺节目的未来发展趋势的摘要怎么写,内容包括论文英文摘要翻译在线等中国电视真人秀节目成功的原因及发展,你怎么看待真人秀节目在未来的发展,电视娱乐节目的发展方向。

知识

怎么在手机qq上写日志

阅读(243)

本文主要为您介绍怎么在手机qq上写日志,内容包括如何用手机在QQ上写日志,在手机QQ上怎么写日志,怎么在手机上写QQ日志。手机qq写日志方法如下:首先要安装手机QQ空间,百度移动应用中心都有下载。2、点击“我的空间”。3、然后点击“日志”。4

知识

易语言空间人气量查询子程序怎么写

阅读(279)

本文主要为您介绍易语言空间人气量查询子程序怎么写,内容包括易语言外挂编辑的子程序名查看qq真怎么写,易语言怎么快速查找子程序被那个程序调用,大大门好,我是新入易语言,想知道写内存这个子程序该怎么写,给。如果真(X=X1)子程序1()如果真(X=

知识

儿童低保申请书怎么写

阅读(280)

本文主要为您介绍儿童低保申请书怎么写,内容包括小孩低保申请书怎样写,请教:残疾儿童低保申请书怎么写,帮儿子申请低保怎么写。递交小孩低保申请书需具备的条件小孩申请农村低保待遇应同时具备下列条件:小孩需持有本县农业居民户口;2、居住在

知识

怯的笔顺怎么写

阅读(238)

本文主要为您介绍怯的笔顺怎么写,内容包括怯的笔顺怎么写,怯字繁体字怎么写,衰的笔顺怎么写。衰的笔顺:点,横,竖,横折,横,横,撇,竖提,撇,捺

知识

社团工作计划书怎么写

阅读(266)

本文主要为您介绍社团工作计划书怎么写,内容包括学生社团工作计划怎么写,社团计划书怎么写,大学社团工作计划书怎么写有范文嘛。原发布者:仙人指路2013-2014学年度上学期初中社团工作计划指导思想为丰富校园文化生活,构建健康、和谐的文化氛

知识

舍不得用英语怎么写

阅读(223)

本文主要为您介绍舍不得用英语怎么写,内容包括舍不得的英文怎么写,舍不得用英文怎么写,舍不得英文单词怎么写。舍不得hate to part with or use; grudge; be loath to part with or leave:

知识

单例模式java主函数怎么写

阅读(213)

本文主要为您介绍单例模式java主函数怎么写,内容包括java中的单例模式的代码怎么写,java单例模式怎么写,如何写一个简单的单例模式。我从我的博客里把我的文章粘贴过来吧,对于单例模式模式应该有比较清楚的解释:单例模式在我们日常的项目中十

知识

javafinally方法怎么写

阅读(238)

本文主要为您介绍javafinally方法怎么写,内容包括finally在java的用法,简述java中final和finally的区别及用法,^^请教JAVA中的异常finally{}语句里面要写什么,有什么作用。try的范围内存在exception的话,就会在exception的位置跳到exception

知识

java的块注释怎么写

阅读(258)

本文主要为您介绍java的块注释怎么写,内容包括java中的几种注释方式,Java的注释开头怎么写,如何写Java文档注释。我看动力节点的java基础大全301集教中的三种注释方法:单行注释 //注释的内容2、多行注释 /**/3、/***/,这种方式和第二种

知识

字符密码怎么写java

阅读(256)

本文主要为您介绍字符密码怎么写java,内容包括JAVA中怎么输入一个字符,用JAVA编写输入用户名和密码,java中如何输入一个字符。import java.util.Scanner;public class Test{public static void main(S

知识

java时间显示代码怎么写

阅读(261)

本文主要为您介绍java时间显示代码怎么写,内容包括java编程中显示日期和时间的代码,JAVA中剩余时间代码怎么写,Java中Date只显示当前时间怎么写。/*** 获取现在时间* * @return返回字符串格式 yyyy-MM-dd HH:mm:ss*/public static Str

知识

java自加100怎么写

阅读(230)

本文主要为您介绍java自加100怎么写,内容包括java中如何写1加到100的程序,java中如何写1加到100的程序,编写java程序从1加到100。public class Array {public static void main(String[] args){for(int i

知识

java中怎么写一个接口

阅读(249)

本文主要为您介绍java中怎么写一个接口,内容包括java编写一个有关接口的程序,java做一个接口,怎样把java的一个类定义成接口。public interface 人 -----------接口;{public void 吃饭();---------- 接口中的方法

知识

java技能及特长怎么写

阅读(335)

本文主要为您介绍java技能及特长怎么写,内容包括程序员的技能专长怎么写,程序员的技能专长怎么写,java软件工程师技能专长怎么写。熟练面向对象编程思想,扎实的Java基础知识,精通Jsp、Servlet、Jdbc下的编程开发。 精通Struts、Hibernate、

[/e:loop]