文件管理 · 2022年7月31日

java生成配置文件|如何为java 程序 写配置文件

⑴ 》用java–swing做一个界面:实现xml配置文件的创建和修改

提供给你一个思路:可以把要修改的xml文件创建一个模板。然后对模板进行创建编辑等操作。如果还需要进一步帮助,请补充说明。

⑵ java怎样提取配置文件

java读取配置文件的几种方法如下:方式一:采用ServletContext读取,读取配置文件的realpath,然后通过文件流读取出来。因为是用ServletContext读取文件路径,所以配置文件可以放入在web-info的classes目录中,也可以在应用层级及web-info的目录中。文件存放位置具体在eclipse工程中的表现是:可以放在src下面,也可放在web-info及webroot下面等。因为是读取出路径后,用文件流进行读取的,所以可以读取任意的配置文件包括xml和properties。缺点:不能在servlet外面应用读取配置信息。方式二:采用ResourceBundle类读取配置信息,优点是:可以以完全限定类名的方式加载资源后,直接的读取出来,且可以在非Web应用中读取资源文件。缺点:只能加载类classes下面的资源文件且只能读取.properties文件。方式三:采用ClassLoader方式进行读取配置信息优点是:可以在非Web应用中读取配置资源信息,可以读取任意的资源文件信息缺点:只能加载类classes下面的资源文件。方法4 getResouceAsStreamXmlParserHandler.class.getResourceAsStream 与classloader不同使用的是当前类的相对路径

⑶ java配置文件怎么写

假设有如下xml配置文件config.xml:<?xml version="1.0" encoding="utf-8" ?><config><name>kiyho</name><name>sinkiang</name><info><age>100</age></info></conifig>可以用以下代码访问: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();}}}

⑷ 怎样在JavaWeb项目中自定义一个配置文件。

你直接写个过滤类就可以了(使用filter的3个方法你懂得)

package com.enation.eop;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.util.Collection;import java.util.Date;import java.util.List;import java.util.Locale;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.enation.eop.processor.back.BackendProcessor;import com.enation.eop.processor.core.freemarker.FreeMarkerPaser;import com.enation.eop.processor.facade.FacadeProcessor;import com.enation.eop.processor.facade.InstallProcessor;import com.enation.eop.processor.facade.ResourceProcessor;import com.enation.eop.resource.IAppManager;import com.enation.eop.resource.model.EopApp;import com.enation.eop.sdk.context.EopContext;import com.enation.eop.sdk.context.EopContextIniter;import com.enation.eop.sdk.context.EopSetting;import com.enation.framework.context.spring.SpringContextHolder;import com.enation.framework.context.webcontext.ThreadContextHolder;public class DispatcherDemoFilter implements Filter { public void init(FilterConfig config) {}public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {// HttpServletRequest req= ServletActionContext.getRequest();// String flag= req.getHeader("X-Requested-With");// HttpServletResponse response = ServletActionContext.getResponse();String ip = ((HttpServletRequest) request).getHeader("x-forwarded-for"); if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){ip = ((HttpServletRequest) request).getHeader("Proxy-Client-IP");} if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){ip = ((HttpServletRequest) request).getHeader("WL-Proxy-Client-IP");} if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){ip = request.getRemoteAddr();}String authtoken = request.getParameter("auth-token");System.out.println(authtoken);StringBuffer sb = new StringBuffer() ;InputStream is = request.getInputStream();InputStreamReader isr = new InputStreamReader(is);BufferedReader br = new BufferedReader(isr);String s = "" ;while((s=br.readLine())!=null){sb.append(s) ;}System.out.println(sb);String str =sb.toString();System.out.println(ip+"时间"+new Date().getTime()); //response.setCharacterEncoding("utf-8");response.setContentType("application/xml");String write = "<cart><items><item feed-id="449573" offer-id="15" price="825" count="1" delivery="true"/>" +"</items><delivery-options><delivery service-name="Курьер" type="DELIVERY" price="1490.00">" +"<dates from-date="24-10-2016" to-date="26-10-2016"/></delivery>" +"</delivery-options><payment-methods>" +"<payment-method>CASH_ON_DELIVERY</payment-method></payment-methods></cart>";System.out.println(write);response.getWriter().write(write);} public void destroy() {}private static boolean isExinclude(String uri) {String[] exts = new String[] { "jpg", "gif", "js", "png", "css", "doc", "xls", "swf","ico" };for (String ext : exts) { if (uri.toUpperCase().endsWith(ext.toUpperCase())) { return true;}} return false;}}

⑸ 怎样读取和写我自己写的一个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 获取配置文件路径

读取配置文件 , xxx.properties放在webroot/WEB-INF/classes/目录下首先将配置文件转换成InputStream,有两种方式,原理一样,都是通过类加载器得到资源: (1)InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("xx.properties"); (2) InputStream inputStream =this.getClass() .getClassLoader().getResourceAsStream( "xx.properties" );调用对象的getClass()方法是获得对象当前的类类型,这部分数据存在方法区中,而后在类类型上调用 getClassLoader()方法是得到当前类型的类加载器,我们知道在Java中所有的类都是通过加载器加载到虚拟机中的,而且类加载器之间存在父 子关系,就是子知道父,父不知道子,这样不同的子加载的类型之间是无法访问的(虽然它们都被放在方法区中),所以在这里通过当前类的加载器来加载资源也就 是保证是和类类型同一个加载器加载的。最后调用了类加载器的getResourceAsStream()方法来加载资源。 (3) 然后加载配置文件,读取属性值 Properties prop = new Properties(); prop.load(input); String value = prop.getProperty("PropertyName"); input.close();

⑺ Java-Myeclipse如何导出配置文件

第一种方法:1.打开myeclipse 选择File=>Import2.在弹出的对话框中选择general=》exitingProject=》next=》Browse3.在弹出的对话框中输专入选择已经存在的Java项目的路径(属选到最外层的文件夹就可以了)=》ok4.在projects中选择需要导入的项目(一般只有你选的这一个)5.选择finish6.到这里为止就已经可以了。第二种方法:1.将项目文件夹拷贝到一个已经存在的工作空间中。(或者随便找个合适的地方新建一个文件夹塞进去)2.打开myeclipse,将工作空间的路径指定为你刚刚新建的那个文件夹。3.选择ok,再ok。在打开的myeclise中你就能够看到那个项目了。

⑻ 如何为java 程序 写配置文件

假设抄有如下xml配置文件config.xml:?xmlversion="1.0"encoding="utf-8"?kiyhosinkiang100可以用以下代码访问:importorg.apache.commons.configuration.configurationexception;importorg.apache.commons.configuration.xmlconfiguration;publicclassxmlconfigdemo{publicstaticvoidmain(string[]args){try{xmlconfigurationconfig=newxmlconfiguration("config.xml");system.out.println(config.getlist("name"));system.out.println(config.getint("info.age"));}catch(configurationexceptione){e.printstacktrace();}}}

⑼ 配置文件在java 中怎么创建

1.一般在scr下面新建一个属性文件*.properties,如a.properties然后在Java程序中读取或操作这个属性文件。代码实例 属性文件a.properties如下:name=rootpass=liukey=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<String> 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"?> <Accounts> <Account type="by0003"> <code>100001</code> <pass>123</pass> <name>李四</name> <money>1000000.00</money> </Account> </Accounts>现在操作config.properties文件。import org.apache.commons.configuration.Configuration; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; public class peropertiesLoaderTest { public static void main(String[] args) throws ConfigurationException{ Configuration config = new PropertiesConfiguration("com/styspace/config.properties"); String name = config.getString("name"); System.out.println("name:" + name); } }

⑽ 如何自定义一套java后台项目配置文件xml,不是j2ee那种

在里面新建一个XML file或者 file,XML file 会自动生成XML头,在下面加入内容就可以了,首先要有一个根节点,然后如果需要用到一些类,如:spring的一些类,就需要引入包,如:<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><context:component-scan base-package="com.dist.*.controller" /> </beans >其中<?xml ……就是头,<beans 是根节点,下面的<content:……是内容。如果添加的事properties文件,格式如下:# 连接池配置pool.size = 2pool.max = 50