JAVA读取propertise文件内容两种方式
2016-01-08 17:58
579 查看
/** * 读取propertise文件 * @throws IOException */ @Test public void fun1() throws IOException { /*方式一【采用反射的方式获取】:最常用的方式。常用于读取同一文件夹下的文件,若不在同一文件夹下,需要往上层找*/ InputStream inputStream = this.getClass().getResourceAsStream("../../../../config.properties");//相对路径 InputStream inputStream = this.getClaa().getClassLoader().getResourceAsStream("华融湘江的ID.txt");//获取类加载路径,读取src下的文件 Properties properties = new Properties(); properties.load(inputStream); System.out.println(properties.getProperty("password"));; /*方式二【采用ResourceBundle来获取本地资源】:只适用于资源文件在src下面的情况*/ ResourceBundle resourceBundle = ResourceBundle.getBundle("config"); System.out.println(resourceBundle.getString("DMUserName")); /*方式三:以文件流的形式读取*/ Properties properties = new Properties(); properties.load(new FileReader("config.properties")); System.out.println(properties.getProperty("password")); System.out.println(URLDecoder.decode(this.getClass().getResource("").getPath(),"utf-8"));//获取当前类所在的项目路径 }
项目结构如下:
public class ReadText { public static void main(String[] args) { try { //FileReader fileReader = new FileReader("E:/银监会/华融湘江的ID.txt"); InputStream in = ReadText.class.getClassLoader().getResourceAsStream("华融湘江的ID.txt"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in, "UTF-8")); StringBuilder builder = new StringBuilder("0"); String str = ""; while((str=bufferedReader.readLine()) != null) { builder.append(","+str); } System.out.println(builder); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
相关文章推荐
- Java的复制详解
- activemq spring整合 以及虚拟主题设置
- Java应用程序可执行jar文件与服务器交互中文乱码
- java发送邮箱
- Java中唯一数的生成
- SpringBoot Demo
- Jdk安装与环境变量配置
- Eclipse设置代码提示
- eclipse快捷键以及使用技巧大全
- hive1.2.1源码导入eclipse阅读以及调试
- Java内存区域及管理
- java生成图片
- springmvc统一异常处理
- Struts2 基于XML配置方式实现对action的所有方法进行校验
- Java DES 加密和解密源码
- 对Ansible二次开发来检查服务器的Java程序占用端口数量
- Java 日志管理最佳实践
- java中Double类数字太大时页面正常显示而不要用科学计数法
- Java - MultipartFile图片上传服务器,并且指定大小压缩
- Hibernate - Spring 整合基础实例 《二》