java使用相对路径操作文件的问题?谢谢!!!

dl1125 2005-04-05 09:21:44
1.目标:
操作和类同一个目录中的文件(读取.修改.新建)(文件名已知)
|--test
|------A.java
|------url.conf
读取已经解决:
InputStream file = this.getClass().getResourceAsStream(fileName);
修改新建如何解决?谢谢!!!

2 .目标:读取类路径下的文件,通过A读取test下面的所有文件,有更好的吗?
|--test
|------A.java
|------url.conf
|------url.conf


目前想到的方案:
public class GetXmlFileforWindowsImp implements IGetXmlFile {
public GetXmlFileforWindowsImp() {
}

//log
private static Logger logger = Logger.getLogger(GetXmlFileforWindowsImp.class);

/**
* 取得Path
* @return String
*/
private String getPath() {
String strPath = "";
String strDomainPath = this.getDomainPath();
String strPackagePath = this.getPackagePath();
if (strDomainPath.indexOf(strPackagePath) == -1) {
strPath = this.getDomainPath() + "/" + this.getPackagePath();
}
else {
strPath = strDomainPath;
}
return strPath;
}

/**
* 取得DomainPath的路径
* @return String
*/
private String getDomainPath() {
String strDomain = this.getClass().getProtectionDomain().toString();
String domainPath = this.parseDomainPath(strDomain);
return domainPath;
}

/**
* 返回DomainPath
* @param string String
* @return String
*/
private String parseDomainPath(String string) {
String strPath = string.substring(string.indexOf("(") + 1, string.indexOf(")"));
int first = strPath.indexOf("/");
int second = strPath.lastIndexOf("/");
String parseDomainPath = strPath.substring(first + 1, second);
return parseDomainPath;
}

/**
* 取得package的路径
* @param string String
* @return String
*/
private String getPackagePath() {
String strpackage = this.getClass().getPackage().toString();
return this.parsePackagePath(strpackage);
}

/**
* 截取package的路径,并进行转换将“.”转换为//
* @param string String
* @return String
*/
private String parsePackagePath(String string) {
String t_packagePath = string.substring(8);
// packagePath.replaceAll(".","\\");
String packagePath = StringUtils.replace(t_packagePath, ".", "/");
return packagePath;
}

/**
* 只截取XMl文件
* @return ArrayList 里面存的的是File对象
*/
public List getXmlFiles() {
logger.info(Constant.OUT);
logger.info("po到vo的转换文件的目录的读取!!");
ArrayList al = new ArrayList();
String Path = this.getPath();
File file = new File(Path);
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].getPath().indexOf(".XML") != -1 || files[i].getPath().indexOf(".xml") != -1) {
logger.info("加载的文件名称:" + files[i]);
al.add(files[i]);
}
}
logger.info(Constant.OUT);
return al;
}

// public static void main(String arg[]) {
// GetXmlFileforWindowsImp get = new GetXmlFileforWindowsImp();
// get.getPath();
// }

}


...全文
637 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
discolt 2005-04-24
  • 打赏
  • 举报
回复
Spring >>

org.springframework.util.ClassUtils;

ClassUtils.addResourcePathToPackagePath(AppContext.class, "applicationContext.xml");
-> /org/orange/context/applicationContext.xml

ClassUtils.classPackageAsResourcePath(AppContext.class);
->/org/orange/context/
YuLimin 2005-04-24
  • 打赏
  • 举报
回复
看看我的Blog,里面有关于这方面的资料

http://iAMin.blogdriver.com
jFresH_MaN 2005-04-24
  • 打赏
  • 举报
回复
其实你的代码不错
而你的问题我觉得也就是一个路径的问题

你可以使用绝对路径
这个this.class.getResource("/");

这个返回的URL就是你的项目的根目录
我一直用这个方法得到路径的,然后再用class名字和package名字就能定位到项目里面的所有文件
新建文件只要File f=new File(path);,反正我们已经得到path了,然后create()方法新建

不过有个值得注意的地方是,上面的方法取得的url一开始有个
file:
这就好象http的路径一开始一样的东西,我们只要用substring()去掉就行了
jFresH_MaN 2005-04-24
  • 打赏
  • 举报
回复
什么问题呢?
这段代码很不错。。
dl1125 2005-04-24
  • 打赏
  • 举报
回复
有人知道吗?谢谢!!!

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧