java 读取文件可以用字节流和字符流。 由于一个汉字占两个字节,所以如果配置文件中有汉字,用字节流读取,会出现乱码。 用字符流则不会出现乱码。

配置文件 b.properties 文件如下:

family\ name = zhou
second name = dingzhao
gender = male
tel no. = +86 (-) 13913462
salary = 0.001k
职业 =java & 工程师

读取配置文件的代码如下:

Properties pro = new Properties();

try {

  FileReader in2 = new FileReader(new File("I:\\Workspaces\\MyWork\\xcserver\\src\\com\\xiaocong\\user\\service\\b.properties"));

  pro.load(in2);  

  System.out.println(pro.getProperty("family name"));

  Iterator<String> it=pro.stringPropertyNames().iterator();
    while(it.hasNext()){
    String key=it.next();
    System.out.println(key+":"+pro.getProperty(key));
  }

  in2.close();

} catch (FileNotFoundException e) {

  e.printStackTrace();

}

System.out.println(pro.getProperty("职业"));

输出:

zhou
second:name = dingzhao
tel:no. = +86 (-) 139133462
family name:zhou
gender:male
salary:0.001k
职业:java & 工程师

java & 工程师

注:

1、配置文件中有空格的话,需要用 \ 转义符。

2、pro.getProperty("职业")   getProperty(key)方法可以取得配置文件中变量的值。

字节流代码如下:

Properties pro = new Properties();

try {

  InputStream in2 = new BufferedInputStream (new FileInputStream("I:\\Workspaces\\MyWork\\xcserver\\src\\com\\xiaocong\\user\\service\\a.properties"));

  pro.load(in2);

  System.out.println(pro.getProperty("family name"));

  Iterator<String> it=pro.stringPropertyNames().iterator();
  while(it.hasNext()){
    String key=it.next();
    System.out.println(key+":"+pro.getProperty(key));
  }

  in2.close();

} catch (FileNotFoundException e) {

  e.printStackTrace();

}

字节流配置文件中的中文会变成乱码。

  

java读取配置文件的更多相关文章

  1. java读取配置文件的几种方法

    java读取配置文件的几种方法 原文地址:http://hbcui1984.iteye.com/blog/56496         在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配 ...

  2. Java读取配置文件的方式

    Java读取配置文件的方式-笔记 1       取当前启动文件夹下的配置文件   一般来讲启动java程序的时候.在启动的文件夹下会有配置文件 classLoader.getResource(&qu ...

  3. Java 读取配置文件数据

    Properties类 Properties类,是一个工具类,包含在java.util包中. 功能:可以保存持久的属性,通常用来读取配置文件或者属性文件,将文件中的数据读入properties对象中, ...

  4. java读取配置文件方法以及工具类

    第一种方式 : java工具类读取配置文件工具类 只是案例代码  抓取异常以后的代码自己处理 import java.io.FileNotFoundException; import java.io. ...

  5. java读取配置文件内容

    利用com.typesafe.config包实现 <dependency> <groupId>com.typesafe</groupId> <artifact ...

  6. spring boot使用java读取配置文件,DateSource测试,BomCP测试,AnnotationConfigApplicationContext的DataSource注入

    一.配置注解读取配置文件         (1)@PropertySource可以指定读取的配置文件,通过@Value注解获取值   实例:           @PropertySource(val ...

  7. 使用Java读取配置文件

    实现起来,相对比较简单,留个备案吧,废话也不多说,请看代码: package com.jd.***.config; import org.junit.*; import java.io.IOExcep ...

  8. java读取配置文件(转)

    转载:http://blog.csdn.net/gaogaoshan/article/details/8605887 java 4种方式读取配置文件 + 修改配置文件     方式一:采用Servle ...

  9. 转:java读取配置文件的几种方法

    转自: http://www.iteye.com/topic/56496 在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配置文件来完成,本文根据笔者工作中用到的读取配置文件的方法小小 ...

随机推荐

  1. sonar-maven-plugin问题

    问题: jenkins本地构建时sonar报错 StackOverflow问题 [ERROR] Failed to execute goal org.codehaus.mojo:sonar-maven ...

  2. 自己编写的sublime text 3 插件

    一些小功能,比较杂. 具体的功能在这里查看 1.本地环境的php运行结果获取. 2.快捷打开常用的文件,文件夹,url.(ctrl+shift+a) 3.常用的缩进转换. 下边是网络爬虫代码. #py ...

  3. hdu 1009 贪心算法

    博主英语不好,看懂个大概,老鼠有M磅猫食.有N个房间,每个房间前有一只猫,房间里有老鼠最喜欢的食品JavaBean,J[i].若要引开猫,必须付出相应的猫食F[i]. 当然这只老鼠没必要每次都付出所有 ...

  4. PDF转WORD工具 Solid Converter PDF v9.1.6744

    Solid Converter PDF中文破解版(pdf转换成word转换器)是一款功能强大的PDF格式转换软件.Solid Converter PDF允许用户将PDF转换为Word(PDF to W ...

  5. 监听iOS检测屏幕旋转状态,不需开启屏幕旋转-b

    -(void)rotation_icon:(float)n { UIButton *history_btn= [self.view viewWithTag:<#(NSInteger)#>] ...

  6. 【POJ 3623】 Best Cow Line, Gold (后缀数组)

    [题意] [分析] 后缀数组水题,嗯,不认真看输出像我一样就会被坑.. #include<cstdio> #include<cstdlib> #include<cstri ...

  7. Eclipse报错:java.lang.ClassNotFoundException: ContextLoaderListener(Maven工程)

    Eclipse中tomcat部署工程启动后报错: 严重: Error configuring application listener of class org.springframework.web ...

  8. [wikioi]乌龟棋

    http://wikioi.com/problem/1068/ 多重背包.边界f[0,0,0,0]=a[1](初始时没有用任何卡片,获得棋盘第一格的分数)DP方程:f[i,j,k,l]=max(f[i ...

  9. QString的不常见用法

    QString str("Hello"); QString str = "Hello"; static const QChar data[4] = { 0x00 ...

  10. android Service开机启动及debug

    开机启动一个service需要做的工作如下: 1.开发一个receiver用于接收系统广播: public class BootReceiver extends BroadcastReceiver { ...