1.java文件读取properties文件

 Properties props = new Properties();
try {
//资源文件存放在类文件的根目录下。即是放在src下面。则不需要写路
      //径,此时是放在file文件夹下
props.load(getClass().getClassLoader().getResourceAsStream(
"file/user.properties"));
//当资源文件中有中文的时候可以采用下面的编码转化方法来读取。
//然后直接读取props.getProperty("name");
System.out.println(new String(props.getProperty("name").getBytes(
"ISO-8859-1"), "GBK"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

2.java读取xml文件

try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
Document doc = db.parse(new File("d://asp-search-config.xml"));
Element elmtInfo = doc.getDocumentElement();
NodeList nodes = elmtInfo.getChildNodes();
int m = 1;
System.out.println(nodes.getLength());
for (int i = 0; i < nodes.getLength(); i++)
{
Node result = nodes.item(i);
System.out.println(result.getNodeType()+"----"+result.getNodeName());
if (result.getNodeType() == Node.ELEMENT_NODE && result.getNodeName().equals("index-file"))
{
NodeList ns = result.getChildNodes(); for (int j = 0; j < ns.getLength(); j++)
{
Node record = ns.item(j);
System.out.println(record.getNodeType()+"@@@@"+record.getNodeName());
if (record.getNodeType() == Node.ELEMENT_NODE && record.getNodeName().equals("path"))
{
System.out.println(m + ": " + record.getTextContent());
m++;
}
}
}
}
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
catch (SAXException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}

第二种方法:

 // 使用了dom4j解析xml
// 读取目录下用来测试的*.xml文件,取得xml主内容
SAXReader saxReader = new SAXReader();
Document document = saxReader.read("d://asp-search-config.xml").getDocument();
int i = 1;
// 遍历文档根节点(wuxialist)下的子节点列表,即txtbook节点的集合
     //document.getRootElement()获取根节点asp-search-config
for(Element txtbook : (List<Element>)document.getRootElement().elements()){
//取得txtbook节点下的name节点的内容
System.out.println(i+"."+txtbook.element("path").getText());
}

3.运用spring读取xml文件

 1.利用ClassPathXmlApplicationContext
  ApplicationContext context = new ClassPathXmlApplicationContext("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直接目录下,或者写明绝对路径,否则就会抛出找不到文件的异常。

4.运用spring读取properties文件

我们还利用上面的HelloBean.java文件,构造如下beanConfig.properties文件:

helloBean.class=chb.demo.vo.HelloBean

helloBean.helloWorld=Hello!chb!

    属性文件中的"helloBean"名称即是Bean的别名设定,.class用于指定类来源。

    然后利用org.springframework.beans.factory.support.PropertiesBeanDefinitionReader来读取属性文件

  BeanDefinitionRegistry reg = new DefaultListableBeanFactory();

  PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(reg);

  reader.loadBeanDefinitions(new ClassPathResource("beanConfig.properties"));

  BeanFactory factory = (BeanFactory)reg;

  HelloBean helloBean = (HelloBean)factory.getBean("helloBean");

  System.out.println(helloBean.getHelloWorld());

5.判断名字为name的cookie是否存在,存在则得到该cookie的值,不存在则创建出值为“程序员”的cookie

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
Cookie[] cookies = request.getCookies();
boolean flag = false ;
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookie.getName().equals("name")) {
out.print("<h2>exist , "+URLDecoder.decode(cookie.getValue(),"utf-8")+"</h2>");
flag = true ;
break;
}
if (!flag) {
Cookie cookieAdd = new Cookie("name", URLEncoder.encode("程序员","utf-8"));
response.addCookie(cookieAdd);
out.print("<h2>not exist , create successfully</h2>");
}
}
}else{
Cookie cookieAdd = new Cookie("name", URLEncoder.encode("程序员","utf-8"));
response.addCookie(cookieAdd);
out.print("<h2>no one exist,create successfully</h2>");
}
out.close();
}

java代码和spring框架读取xml和properties文件的更多相关文章

  1. 【Java编程】写入、读取、遍历Properties文件

    在Java开发中通常我们会存储配置參数信息到属性文件.这种属性文件能够是拥有键值对的属性文件,也能够是XML文件.关于XML文件的操作,请參考博文[Java编程]DOM XML Parser 解析.遍 ...

  2. spring 框架的xml文件如何读取properties文件数据

    spring 框架的xml文件如何读取properties文件数据 第一步:在spring配置文件中 注意:value可以多配置几个properties文件 <bean id="pro ...

  3. Java 横向技术 Spring框架【笔记】

    Java横向技术 spring框架[笔记] Spring 的两大特性是什么? AOP(Aspect Oriented Programming,面向切面编程)与 IOC(Inverse of Contr ...

  4. 基于纯Java代码的Spring容器和Web容器零配置的思考和实现(3) - 使用配置

    经过<基于纯Java代码的Spring容器和Web容器零配置的思考和实现(1) - 数据源与事务管理>和<基于纯Java代码的Spring容器和Web容器零配置的思考和实现(2) - ...

  5. 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)

    Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式   √ id:标识容器中的bean.id唯一. √ cl ...

  6. java如何读取和遍历properties文件

    在java项目开发过程中,使用properties文件作为配置基本上是必不可少的,很多如系统配置信息,文件上传配置信息等等都是以这种方式进行保存.同时学会操作properties文件也是java基础. ...

  7. 【Java】关于Spring框架的总结 (一)

    本文总结一些关于Spring框架的理解,注意点及基础操作.如果有不对的地方,欢迎批评和建议.大家一起努力吧! Spring 框架简介 Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创 ...

  8. 【Java】关于Spring框架的总结 (三)

    前文对 Spring IoC 和 Spring AOP 的实现方法进行了整合.如果有不明白的或有质疑的地方可以评论出来,一起探讨问题,帮助别人也是帮助自己!本文探讨的中心主要放在 Spring 的注解 ...

  9. Spring如何读取xml配置文件的

    我们通过一个小案例来看xml解析过程. 1. 导包 <dependencies> <!-- xml解析工具 --> <dependency> <groupId ...

随机推荐

  1. WinForm------GridControl显示每行的Indicator中的行号

    1.修改Indicator的行宽 2.添加CustomDrawRowIndicator事件 private void AdminCardView_CustomDrawRowIndicator(obje ...

  2. bootstrap弹框

    http://v3.bootcss.com/javascript/#modals 参考bootstrap官网 模态框做php后端 前端一直不行,但是很多时候 用到ajax都要用到弹框,一直在代码里面找 ...

  3. php基础语句2

    计算一个月有多少天 $count = date("t",strtotime("2014-09-01")); // 一个月有多少天

  4. JavaScript中变量和函数声明的提升

    现象: 1.在JavaScript中变量和函数的声明会提升到最顶部执行. 2.函数的提升高于变量的提升. 3.函数内部如果用var声明了相同名称的外部变量,函数将不再向上寻找. 4.匿名函数不会提升. ...

  5. mysql的Replication机制

    mysql的Replication机制 参考文档:http://www.doc88.com/p-186638485596.html Mysql的 Replication 是一个异步的复制过程. 从上图 ...

  6. app接口的简单案例 和一些总结

    例一: 通过接口获取一篇文章.接口需要传入文章的id,通过sql语句向数据库查询文章的内容,然后以json的格式echo出即可,即:安卓或IOS工程师获取通过接口获取到了json格式的数据,在做进一步 ...

  7. 让Xcode的控制台支持LLDB类型的打印

    这个技巧个人认为非常有用 当Xcode在断点调试的时候,在控制台中输入 po self.view.frame 类似这样的命令会挂掉,不信可以亲自去试试(Xcode7 以后支持LLDB类型的打印) 那么 ...

  8. JS网页加载进度条

    参考:http://www.cnblogs.com/timy/archive/2011/12/07/2279200.html

  9. post、get的区别

    get的参数会显示在浏览器地址栏中,而 post的参数不会显示在浏览器地址栏中: 使用 post提交的页面在点击[刷新]按钮的时候浏览器一般会提示“是否重新提交”,而 get则不会: 用get的页面可 ...

  10. hdu4976 A simple greedy problem. (贪心+DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=4976 2014 Multi-University Training Contest 10 1006 A simp ...