java 读取src目录下的配置文件
来源:互联网 发布:mac网络恢复 分区 编辑:程序博客网 时间:2024/05/25 08:15
原文转自:http://www.tqcto.com/article/code/295339.html
目前的代码如下:
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.UnsupportedEncodingException;
- import java.net.URLDecoder;
- import java.util.Iterator;
- import java.util.Properties;
- import java.util.Set;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author zcb
- */
- public class Test {
- public static void main(String args[]) {
- Test test = new Test();
- InputStream in = null;
- Properties props = new Properties();
- //第一种方法,取得src下的属性文件,成功
- in = test.getClass().getResourceAsStream("/mypropertiestest.properties");
- //第二种方法,取得src下的属性文件,相对第一种少了个“/”,注意:error,不行,此时取得的路径是到classes文件夹
- // System.out.println("path:"+test.getClass().getResource("mypropertiestest.properties").getPath());
- // in = test.getClass().getResourceAsStream("mypropertiestest.properties");
- //第三种种方法,通过绝对路径,取得src下的属性文件,成功,但对apusic服务器不大理想,属性文件要拷贝到项目外面
- // String filepath = test.getClass().getResource("/").getPath() + java.io.File.separator + "mypropertiestest.properties";
- // try {
- //// filepath = filepath.substring(1).replaceAll("%20", " ");//or filepath = filepath.replaceAll("%20", " ");
- // filepath = URLDecoder.decode(filepath, "UTF-8");
- // } catch (UnsupportedEncodingException ex) {
- // Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
- // }
- // try {
- // in = new FileInputStream(new File(filepath));
- // } catch (FileNotFoundException ex) {
- // Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
- // }
- try {
- props.load(in);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (in != null) {
- try {
- in.close();
- } catch (IOException ex) {
- Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- //输出属性文件中的信息
- Set set = props.keySet();
- Iterator it = set.iterator();
- System.out.println("Begin ...");
- while (it.hasNext()) {
- String key = (String) it.next();
- System.out.println(key + "=" + props.getProperty(key));
- }
- System.out.println("End");
- }
- }
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.UnsupportedEncodingException;
- import java.net.URLDecoder;
- import java.util.Iterator;
- import java.util.Properties;
- import java.util.Set;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author zcb
- */
- public class Test {
- public static void main(String args[]) {
- Test test = new Test();
- InputStream in = null;
- Properties props = new Properties();
- //第一种方法,取得src下的属性文件,成功
- in = test.getClass().getResourceAsStream("/mypropertiestest.properties");
- //第二种方法,取得src下的属性文件,相对第一种少了个“/”,注意:error,不行,此时取得的路径是到classes文件夹
- // System.out.println("path:"+test.getClass().getResource("mypropertiestest.properties").getPath());
- // in = test.getClass().getResourceAsStream("mypropertiestest.properties");
- //第三种种方法,通过绝对路径,取得src下的属性文件,成功,但对apusic服务器不大理想,属性文件要拷贝到项目外面
- // String filepath = test.getClass().getResource("/").getPath() + java.io.File.separator + "mypropertiestest.properties";
- // try {
- //// filepath = filepath.substring(1).replaceAll("%20", " ");//or filepath = filepath.replaceAll("%20", " ");
- // filepath = URLDecoder.decode(filepath, "UTF-8");
- // } catch (UnsupportedEncodingException ex) {
- // Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
- // }
- // try {
- // in = new FileInputStream(new File(filepath));
- // } catch (FileNotFoundException ex) {
- // Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
- // }
- try {
- props.load(in);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (in != null) {
- try {
- in.close();
- } catch (IOException ex) {
- Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- //输出属性文件中的信息
- Set set = props.keySet();
- Iterator it = set.iterator();
- System.out.println("Begin ...");
- while (it.hasNext()) {
- String key = (String) it.next();
- System.out.println(key + "=" + props.getProperty(key));
- }
- System.out.println("End");
- }
- }
在windows下测试通过,Linux没测试,需要进一步研究。
补充:使用ClassLoader.getSystemResourceAsStream("/mypropertiestest.properties")和Thread.currentThread().getContextClassLoader().getResourceAsStream("/mypropertiestest.properties")读取src下的属性文件,通过测试,在windows和Linux下的tomcat和apusic都能成功编程技术。
0 0
- java读取src目录下的配置文件
- Java读取src目录下的配置文件
- java 读取src目录下的配置文件
- java 读取src目录下配置文件
- Java读取src目录下的properties配置文件
- java读取src下的配置文件
- java读取src下的配置文件
- java 读取src下的配置文件
- jsp项目读取src目录下的配置文件
- 读取 src 目录下 自定义配置文件
- 读取src下的配置文件
- Java在src目录下加载配置文件的方法
- web项目 读取src下的配置文件
- 关于java读取src配置文件的问题
- Maven项目中读取src/main/resources目录下的配置文件
- java读取src下面的目录
- scala学习-12-scala读取java项目下Src目录下的properties文件
- java web项目获取src和WebContent目录下的配置文件
- Spring MVC源码深入剖析执行流程
- MCC(移动国家码)和 MNC(移动网络码)
- 疯狂猜歌实现(5)—— 实现待选文字框动画以及点击事件
- 《高性能MySQL》第五章
- Android Volley的基本用法
- java 读取src目录下的配置文件
- linux下如何从终端打开文件夹,并且获取管理员权限
- Asp.Net 小型网站的制作(六),涵盖了asp.net里面所有的基础知识
- POJ 2048 Longge's problem (欧拉函数 积性函数)
- copy,assign,strong,retain,weak,readonly,nonatomic的区别
- SQLServer - 约束
- 关于findbyId一个view以后,这个view还是报空指针的错误
- 二维数组作为函数参数
- 深入浅出javascript闭包的用途和几种写法