eclipse下的文件路径问题
2010-09-26 17:32
169 查看
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);
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下tomcat上传文件路径的问题
- java文件路径问题及Eclipse package,source folder,folder区别及相互转换
- 关于eclipse里的jsp编译文件路径问题
- 用eclipse写web项目中jsp导入图片和css文件的路径问题
- java在eclipse项目中读取文件时相对路径的问题
- eclipse 利用windows资源管理器打开当前文件所在目录 | eclipse下的Java工程路径问题
- 用eclipse创建tomcat项目后,文件上传目录路径问题
- eclipse 文件路径问题
- web工程在eclipse中的类编译路径与发布后的类编译路径问题和web工程中的资源文件读写问题
- eclipse下的文件路径问题
- eclipse使用generator生成文件路径报错问题
- C# 路径,文件,目录,IO常见问题汇总
- Eclipse进行C开发---无法包含头文件问题
- IE8上传文件时获取文件本地路径问题(C:\fakepath\……)的解决办法
- gdbserver 远程调试问题:设置文件和so搜索路径
- Eclipse下好使,但是cmd下执行java运行class文件提示Exception in thread "main" java.lang.NoClassDefFoundError问题
- Android开发中遇到的问题(三)——eclipse创建android项目无法正常预览布局文件
- tar打包文件时提示绝对路径的问题
- 解决eclipse中设置SDK路径时ADT和SDK版本不一致问题
- JavaWeb中读取文件资源的路径问题 -- 转自新浪博客