eclipse下的文件路径问题
来源:互联网 发布:android 网络ip电话 编辑:程序博客网 时间:2024/04/27 15:48
public class PropertiesGetting {
private Properties properties;
/*
* A constructor is built.
*/
public PropertiesGetting(String fileName)throws Exception{
properties = new Properties();
System.out.println();
InputStream inputstream=new FileInputStream(fileName);
properties.load(inputstream);
}
/*
* A method for getting a value by a specified key
* is implemented.
*/
public String getValue(String key)throws Exception{
String result;
result=properties.getProperty(key);
if (result==null){
throw new Exception("Getting properties from a property file failure!The key can not be found");
}
return result;
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/*
* This class provides a general method to get the
* value of a key from a specified properites file.
*/
public class PropertiesGetting {
private Properties properties;
/*
* A constructor is built.
*/
public PropertiesGetting(String fileName)throws Exception{
properties = new Properties();
//InputStream inputstream=new FileInputStream(fileName);
InputStream inputstream = this.getClass().getResourceAsStream("/"+fileName);
properties.load(inputstream);
inputstream.close();
}
/*
* A method for getting a value by a specified key
* is implemented.
*/
public String getValue(String key)throws Exception{
String result;
result=properties.getProperty(key);
if (result==null){
throw new Exception("Getting properties from a property file failure!The key can not be found");
}
return result;
}
public Properties getProperties(){
return properties;
}
}
这两个类,如果都只是放在eclipse/workspace/project/src的一个包下面,只要把要读取的文件放到src下就没有问题。
但是, 为了日后便于操作,我把这个类文件封装成了一个jar包,作为我的utility。这两个类唯一的区别在于:
InputStream inputstream=new FileInputStream(fileName);
InputStream inputstream = this.getClass().getResourceAsStream("/"+fileName);
打成jar包后,前一种在不同的环境下得到的文件的当前路径不同,比如在ecliipse环境下,如果是java项目则在项目根目录下;如果是j2ee项目则在eclipse目录下(很奇怪!);如果是制作成war包,则应该放到“tomcat”根目录下,并不是tomcat的webapp/projec 下。所以变数很多!
后者相对来说比较固定,就在eclipse的project/src目录下。打成war包应该也不会变(需要测试!)。。。
考虑到项目需要打包,只能把配置文件放到项目根目录下。用如下方法。
//The following method requires the file is in the root directory of project.
String projectAbsolutePath = System.getProperty( "user.dir");
if (System.getProperty("os.name").contains("Windows")) {
fileName = projectAbsolutePath+"\\"+fileName;
}else {
fileName = projectAbsolutePath+"/"+fileName;
}
InputStream inputstream = new FileInputStream(fileName);
- eclipse下的文件路径问题
- eclipse下的文件路径问题
- eclipse下tomcat上传文件路径的问题
- eclipse 利用windows资源管理器打开当前文件所在目录 | eclipse下的Java工程路径问题
- eclipse 文件路径问题
- Eclipse 下 Applet 的路径问题
- 关于eclipse里的jsp编译文件路径问题
- eclipse下的links文件配置问题
- eclipse编辑器下显示文件路径
- IE和Firefox下的文件路径问题
- Tomcat环境下,java读取XML文件的路径问题
- 获取apusic下class文件路径的问题
- Linux和window下的文件路径问题
- Windows环境下Python代码的文件路径问题
- Java项目下exe文件的路径问题及运行
- WEB-INF下jsp文件的路径问题
- Linux和window下的文件路径问题
- Linux下MySQL的my.cnf文件路径问题
- POJ 3469 Dual Core CPU //最小割
- Spring定时器配置
- 红色警报:WORM_STUXNET.A病毒正在流行
- songbank折腾手记(一)----jtag口的使用
- C语言获取汉字拼音首字母
- eclipse下的文件路径问题
- ASP.NET ajax 之updatepanel 失效
- 教你MySQL数据库中字符串的处理方法
- DNS解析协议的C语言简单实现
- Ubuntu学习实用网站(不断更新)
- HTTP代理原理及Base64编码
- 如何在Linux下快速重置MySQL用户密码方法
- MSXML2家族探秘
- Clone使用方法详解