简体   繁体   English

我如何引用Java中的目录?

[英]How do I refer to a directory in Java?

I'm running Windows and I'm trying to refer to a directory. 我正在运行Windows而我正在尝试引用一个目录。 My function starts off like this: 我的功能就像这样开始:

File file = new File("C:\\somedir\\report");
if (!file.exists()) {
  file.mkdirs();
}
doStuffWith(file);

I got a NullPointerException within the doStuffWith function, when I tried to call listFiles . 我得到了内一个NullPointerException doStuffWith功能,当我试图打电话给listFiles Well I looked in C:\\somedir and what did I find - there is a file called "report" with no extension, and also a directory called "report"! 好吧,我查看了C:\\ somedir,我发现了什么 - 有一个名为“report”的文件,没有扩展名,还有一个名为“report”的目录! What seemed to happen was that the file object was referring to the report file rather than the directory . 似乎发生的是file对象是指报告文件而不是目录 How do I make sure that I am referring to the directory and not the file? 如何确保我指的是目录而不是文件?

i think there is a isDirectory() method that will tell you if it is a directory 我认为有一个isDirectory()方法会告诉你它是否是一个目录

--EDIt - 编辑

that's what I get for being up so early. 这就是我这么早起床的原因。 I ran your code locally and it works fine for me. 我在本地运行你的代码,它对我来说很好。 Was able to create new files, read directory contents, etc. What else are you trying to do? 能够创建新文件,读取目录内容等。您还想做什么?

one way to go about is to pass the file object corresponding to "C:\\somedir" to the method and inside the method, do a listFiles() and walk through the contents, each time checking for file name and if it is "report", do a isDirectory(). 一种方法是将对应于“C:\\ somedir”的文件对象传递给方法和方法内部,执行listFiles()并遍历内容,每次检查文件名,如果是“报告” “,做一个isDirectory()。 proceed with actual processing when this returns true. 返回true时继续进行实际处理。

I don't understand the problem this works fine for me: 我不明白这个问题对我来说很好:

public class MkDir {
    static void doStuff(File dir) {
        if ( dir.isDirectory() ) {
            File[] listFiles = dir.listFiles();
            for ( File f : listFiles ) {
                System.out.println( f.getName() );
            }
        }
    }

    public static void main(String[] args) {
        File file = new File( "C:\\dev\\rep2\\rep" );
        if ( !file.exists() ) {
            file.mkdirs();
        }
        doStuff( file );
    }
}

Check if your file system has had case sensitivity enabled ( HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\kernel\\ dword:ObCaseInsensitive in the registry.) 检查您的文件系统是否已启用区分大小写( HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\kernel\\ dword:ObCaseInsensitive注册表中的HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\kernel\\ dword:ObCaseInsensitive )。

If so, you may be getting bitten by a case-related issue. 如果是这样,您可能会因案件相关问题而受到伤害。 One way to check: 一种检查方法:

String someName = "./nameNotUsedYet";
boolean first = new File(someName).mkdirs();
boolean second = new File(someName.toUpperCase()).mkdirs();
System.out.println("first = " + first + ", second = " + second);

If both mkdirs() calls succeeded, you know you have a case related complication. 如果两个mkdirs()调用都成功,则表示您有与案例相关的并发症。 If so, ensure that you get the case for "C:\\somedir\\report" exactly right. 如果是这样,请确保您完全正确地获得"C:\\somedir\\report"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM