在java中获取文件的完整路径 [英] Get file full path in java

查看:1019
本文介绍了在java中获取文件的完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将文件文件传递给方法时,我正试图获取其完整路径,如 file.getAbsolutePath(); 我总是得到相同的结果,无论我使用绝对路径或规范路径 PATH_TO_MY_WORKSPACE / projectName / filename 而且它不在那里,我怎样才能得到确切的位置该文件?

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?

谢谢

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));
                        }
                    }
                }           
            }
        }

我正在循环通过其中factDir为 src / main 的目录,我正在寻找只有TEMP_COMPARE_FILE值的BeProcessed.txt文件,我将它们发送到读取文件并对其进行处理的处理方法。

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

推荐答案

来自 Javadoc 可能会有所帮助:


路径名,无论是抽象的还是字符串形式,都可以是绝对相对。绝对路径名是完整的,因为不需要其他信息来定位它表示的文件。相反,相对路径名必须根据从其他路径名获取的信息来解释。默认情况下, java.io 包中的类始终解析当前用户目录的相对路径名。此目录由系统属性 user.dir 命名,通常是调用Java虚拟机的目录。

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.

我解释这个,这样如果用<$创建文件对象c $ c>新文件(文件名)其中文件名是相对路径,即使是路径也不会转换为绝对路径调用 file.getAbsolutePath()

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:


  • 你可以使用 FilenameFilter 查找所需文件,

  • note对于非目录对象, list listFiles 返回 null ,所以我们需要额外检查一下,

  • 你也可以在内循环中再次使用 listFiles(),从而避免了需要使用手工组装路径创建新的文件对象。 (顺便说一下,手动将 \\ 附加到路径上是不可移植的;正确的方法是使用 File.separator )。

  • 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).

最终结果是

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);
  }
}

请注意,此代码模仿原始作品的行为代码和我理解的一样;最值得注意的是,它只在 factDir 直接子目录中找到具有正确名称的文件,非递归。

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.

这篇关于在java中获取文件的完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆