java中如何创建和读取配置文件|Java里如何添加自定义的配置文件JSP里去读取参数

❶ java怎么在包中创建配置文件

先看代码目录结构:src/weather/QueryWeather.javaweather.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.classweather.xml用下面的方法就可以找到weather.xmlprivate 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中如何设置读取ini配置文件

/* * IniReader.java * 用Java读取INI文件(带section的) * 示例: * IniReader reader = new IniReader("E:\\james\\win.ini"); * out.println(reader.getValue("TestSect3", "kkk 6")); */ package tmp; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Properties; public class IniReader { protected HashMap sections = new HashMap(); private transient String currentSecion; private transient Properties current; public IniReader(String filename) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(filename)); read(reader); reader.close(); } protected void read(BufferedReader reader) throws IOException { String line; while ((line = reader.readLine()) != null) { parseLine(line); } } protected void parseLine(String line) { line = line.trim(); if (line.matches("\\[.*\\]")) { currentSecion = line.replaceFirst("\\[(.*)\\]", "$1"); current = new Properties(); sections.put(currentSecion, current); } else if (line.matches(".*=.*")) { if (current != null) { int i = line.indexOf('='); String name = line.substring(0, i); String value = line.substring(i + 1); current.setProperty(name, value); } } } public String getValue(String section, String name) { Properties p = (Properties) sections.get(section); if (p == null) { return null; } String value = p.getProperty(name); return value; } }ini文件如下: [TestSect1] kkk 1=VVVVVVVVVVV1 kkk 2=VVVVVVVVVVV2 [TestSect2] kkk 3=VVVVVVVVVVV3 kkk 4=VVVVVVVVVVV4 [TestSect3] kkk 5=VVVVVVVVVVV5 kkk 6=VVVVVVVVVVV6

❸ 配置文件在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怎样提取配置文件!怎么才能采用ServletContext读取

创建配置文件:1、在项目的任意地方,右键-》New-》File-》FileName-》输入-》名称.properties(比如:config.properties)2、访问路径:从根目录开始出发(WebRoot)->WEB-INF->classes->config.properties,(如果有包名,在classes->包名->config.properties)(路径可以直接从本地中项目的路径,找到WEB-INF直接从地址中(比如我的本地磁盘保存是这样的:F:\课程\s2课程\s2书上内容\Java Web\ServletTest\WebRoot\WEB-INF\classes\config.properties))response.setContentType("text/html");response.setCharacterEncoding("utf-8");request.setCharacterEncoding("utf-8");PrintWriter out = response.getWriter();/************************使用servletContext.getResourceAsStream**************************************///实例化ServletContextServletContext servletContext=this.getServletContext();// //获取输入流// InputStream in=servletContext.getResourceAsStream("\\WEB-INF\\classes\\config.properties");// Properties p=new Properties();// //类的装载// p.load(in);// //拿到配置文件中userName参数// out.println(p.getProperty("userName"));/***************************普通的获取配置文件**************************************/String path= servletContext.getRealPath(("\\WEB-INF\\classes\\config.properties"));//拿到绝对路径FileInputStream in=new FileInputStream(path);Properties p=new Properties();p.load(in);out.println(p.get("userName"));

❺ 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里如何添加自定义的配置文件,jsP里去读取参数

java里可以再在resources里面新建一个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然后jsp调用读取xml文件的方法去读取自重的内容就可以了。

❼ Java中spring读取配置文件的几种方法

Java中spring读取配置文件的几种方法如下:一、读取xml配置文件 (一)新建一个java beanpackage chb.demo.vo;public class HelloBean { private String helloWorld; public String getHelloWorld() { return helloWorld; } public void setHelloWorld(String helloWorld) { this.helloWorld = helloWorld; }} (二)构造一个配置文件<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" ><beans> <bean id="helloBean" class="chb.demo.vo.HelloBean"> <property name="helloWorld"> <value>Hello!chb!</value> </property> </bean></beans> (三)读取xml文件 1.利用ApplicationContext context = new ("beanConfig.xml");//这种用法不够灵活,不建议使用。 HelloBean helloBean = (HelloBean)context.getBean("helloBean"); System.out.println(helloBean.getHelloWorld()); 2.利用FileSystemResource读取Resource rs = new FileSystemResource("D:/software/tomcat/webapps/springWebDemo/WEB-INF/classes/beanConfig.xml"); BeanFactory factory = new XmlBeanFactory(rs); HelloBean helloBean = (HelloBean)factory.getBean("helloBean"); System.out.println(helloBean.getHelloWorld()); 值得注意的是:利用FileSystemResource,则配置文件必须放在project直接目录下,或者写明绝对路径,否则就会抛出找不到文件的异常。 二、读取properties配置文件 这里介绍两种技术:利用spring读取properties 文件和利用java.util.Properties读取 (一)利用spring读取properties 文件 我们还利用上面的HelloBean.java文件,构造如下beanConfig.properties文件:helloBean.class=chb.demo.vo.HelloBeanhelloBean.helloWorld=Hello!chb! 属性文件中的"helloBean"名称即是Bean的别名设定,.class用于指定类来源。 然后利用org.springframework.beans.factory.support.来读取属性文件 BeanDefinitionRegistry reg = new DefaultListableBeanFactory(); reader = new (reg); reader.loadBeanDefinitions(new ClassPathResource("beanConfig.properties")); BeanFactory factory = (BeanFactory)reg; HelloBean helloBean = (HelloBean)factory.getBean("helloBean"); System.out.println(helloBean.getHelloWorld()); (二)利用java.util.Properties读取属性文件 比如,我们构造一个ipConfig.properties来保存服务器ip地址和端口,如:ip=192.168.0.1port=8080 则,我们可以用如下程序来获得服务器配置信息: InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ipConfig.properties"); Properties p = new Properties(); try { p.load(inputStream); } catch (IOException e1) { e1.printStackTrace(); }System.out.println("ip:"+p.getProperty("ip")+",port:"+p.getProperty("port")); 三 、用接口类WebApplicationContext来取。 private WebApplicationContext wac; wac =WebApplicationContextUtils.( this.getServletContext()); wac = WebApplicationContextUtils.getWebApplicationContext( this.getServletContext()); JdbcTemplate jdbcTemplate = (JdbcTemplate)ctx.getBean("jdbcTemplate");其中,jdbcTemplate为spring配置文件中的一个bean的id值。这种用法比较灵活,spring配置文件在web中配置启动后,该类会自动去找对应的bean,而不用再去指定配置文件的具体位置。

❽ java代码中怎么读取配置文件

需要使用的是jar包:commons-collections-3.2.1.jar、commons-configuration-1.10.jar、commons-lang-2.6.jar和commons-logging-1.2.jar。可以读取的配置文件:xml和properties

❾ Java读取配置文件的几种方法以及路径问题

.类加载器读取:只能读取classes或者类路径中的任意资源,但是不适合读取特别大的资源。 ①获取类加载器 ClassLoader cl = 类名.class.getClassLoader(); ②调用类加载器对象的方法:public URL getResource(String name); 此方法查找具有给定名称的资源,资源的搜索路径是虚拟机的内置类加载器的路径。 类 URL 代表一个统一资源定位符,它是指向互联网”资源”的指针。 资源可以是简单的文件或目录,也可以是对更为复杂的对象的引用. URL对象方法:public String getPath(),获取此 URL 的路径部分。 示例代码:2.类加载器读取:只能读取classes或者类路径中的任意资源,但是不适合读取特别大的资源。 ①获取类加载器 ClassLoader cl = 类名.class.getClassLoader(); ②调用类加载器对象的方法:public InputStream getResourceAsStream(String name); 返回读取指定资源的输入流。资源的搜索路径是虚拟机的内置类加载器的路径。

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


赞 (0)