java file 完整路径,在java中获取文件的完整路径

When I pass File file to a method I'm trying to get its full path like file.getAbsolutePath(); I always get the same result no matter which one I use either absolute or canonical path PATH_TO_MY_WORKSPACE/projectName/filename and it is not there, how can I get exact location of the file?

Thank you

DETAILS:

Here is some code and this solutions(its bad but its working):

private static void doSomethingToDirectory(File factDir) throws IOException {

File[] dirContents = factDir.listFiles();

if(factDir.isDirectory() && dirContents.length > 0){

for (int i = 0; i < dirContents.length; i++) {

for (String str : dirContents[i].list()) {

if(str.equals(TEMP_COMPARE_FILE)){

process(new File(dirContents[i].getAbsolutePath() + "\\" + str));

}

}

}

}

}

I'm looping trough directories where factDir is src/main, I'm seeking toBeProcessed.txt files only that is TEMP_COMPARE_FILE value and I'm sending them to process method which reads the file and does processing of it.

If someone could better solution I'd be greatful

解决方案

This quote from the Javadoc might be helpful:

A pathname, whether abstract or in string form, may be either absolute or relative. An absolute pathname is complete in that no other information is required in order to locate the file that it denotes. A relative pathname, in contrast, must be interpreted in terms of information taken from some other pathname. By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.

I interpret this so that if you create your File object with new File("filename") where filename is a relative path, that path will not be converted into an absolute path even by a call to file.getAbsolutePath().

Update: now that you posted code, I can think of some ways to improve it:

you could use a FilenameFilter to find the desired files,

note that list and listFiles return null for non-directory objects, so we need an extra check for that,

you could also use listFiles() again in the inner loop, thus avoiding the need to create new File objects with hand-assembled paths. (Btw note that appending \\ manually to the path is not portable; the proper way would be to use File.separator).

The end result is

private static void doSomethingToDirectory(File factDir) throws IOException {

if (factDir.isDirectory()) {

for (File file : factDir.listFiles()) {

if (file.isDirectory()) {

for (File child : file.listFiles(new MyFilter())) {

process(child);

}

}

}

}

}

class MyFilter implements FilenameFilter {

public boolean accept(File dir, String name) {

return name.equals(TEMP_COMPARE_FILE);

}

}

Note that this code mimics the behaviour of your original piece of code as much as I understood it; most notably, it finds the files with the proper name only in the direct subdirectories of factDir, nonrecursively.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值