为什么java.io.File.listFiles()抛出NPE而不是正确的异常? [英] Why does java.io.File.listFiles() throw NPE instead of proper Exception?

查看:312
本文介绍了为什么java.io.File.listFiles()抛出NPE而不是正确的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我写了一个小型Java程序来查找我的硬盘上的文件,部分原因是Windows缺少适当的文件,部分原因是因为它很有趣。它只是循环遍历目录的所有子节点,如果它们是目录,则递归地遍历所有子项。但是后来我有一个 NullPointerException 。经过一些 System.out.println()的和一个 try-catch块我发现这发生在一些目录,如 D:\System卷信息 C:\Users\Public\Documents\My视频 C:\ProgramData\Templates 。 Windows本身表示,当在Explorer地址栏中输入访问被拒绝时。当您探索时,他们似乎被锁定。 来源 java.io.File 告诉我:

  public File [] listFiles(){
String [] ss = list();
if(ss == null)return null; //这行似乎是导致NPE
int n = ss.length;
文件[] fs = new File [n]; (int i = 0; i fs [i] = new File(ss [i],this);

}
return fs;
}

我的问题是,为什么没有正确的说法, IOException SecurityException ,甚至一个 IDontCareWhatKindOfExceptionButPleaseNoNullPointerExceptionException



我知道我可以 try-catch NPE ,或检查 null ,但我真的好奇为什么没有正确的异常抛出。



由于我不喜欢 try / catch 并检查 null 因为代码的美学价值,我做了一些令人讨厌的,令人讨厌的反思代码(很多虚伪的 try / catch 块),它模仿了一些方法从 java。 io.File ,因为它们是私有的,一些包(如 java.io.FileSystem )不可见。因为我不在乎一些Windows文件,所以我只是忽略它们使用这个代码,给我一个空的数组

  public static File [] listFiles(File mThis){
String [] ss = list(mThis);
if(ss == null){
//System.out.println(\"ss为null);
return new File [] {};
}
int n = ss.length;
文件[] fs = new File [n]; (int i = 0; i fs [i] = getNewFile(ss [i],mThis);

}
return fs;
}

编辑:避免误解:我使用 try / catch ,我只是不喜欢他们美学。



EDIT2:我宁愿使用一个GUI比命令提示符。我非常不喜欢Windows Search GUI。

解决方案

简答:




  • 安装JDK 7;

  • 使用新的文件 API。



    • 之后,您将永远不会再回到文件


      Today I wrote a small Java program to find files on my harddisk, partly because Windows lacks a proper one and partly because it was for fun. It simply iterates over all childs of a directory and recursively through all their children if they are directories. But then I had a NullPointerException. After some System.out.println()'s and a try-catch blockI found out this happened on some directories like D:\System Volume Information, C:\Users\Public\Documents\My Videos and C:\ProgramData\Templates. Windows itself says, when input in the Explorer address bar "Access denied". When exploring, they seem to be locked. The source from java.io.File tells me:

      public File[] listFiles() {
          String[] ss = list();
          if (ss == null) return null; //This line seems to cause the NPE
          int n = ss.length;
          File[] fs = new File[n];
          for (int i = 0; i < n; i++) {
              fs[i] = new File(ss[i], this);
          }
          return fs;
      }
      

      My question is, why is there no proper say, IOException, SecurityException, or even an IDontCareWhatKindOfExceptionButPleaseNoNullPointerExceptionException?

      I know I can just try-catch for NPEs, or check for null, but I'm really curious of why there's no proper Exception thrown.

      Since I dislike try/catch and checking for null because of the aesthetic value of code, I made some nasty, nasty reflection code (with lots of hypocritical try/catch blocks) which emulates some methods from java.io.File, since they're private, and some packages (like java.io.FileSystem) are not visible. And because I do not care about some Windows files, I simply ignore them using this code, giving me an empty array:

      public static File[] listFiles(File mThis) {
          String[] ss = list(mThis);
          if (ss == null) {
              //System.out.println("ss is null");
              return new File[] {};
          }
          int n = ss.length;
          File[] fs = new File[n];
          for (int i = 0; i < n; i++) {
              fs[i] = getNewFile(ss[i], mThis);
          }
          return fs;
      }
      

      EDIT: to avoid misinterpretations: I do use try/catch, I merely don't like them aesthetically.

      EDIT2: I'd rather use a GUI than the command prompt. And I very much dislike the Windows Search GUI. It's almost painful to use.

      解决方案

      Short answer:

      • install JDK 7;
      • use the new Files API.

      You will never come back to File after that.

      这篇关于为什么java.io.File.listFiles()抛出NPE而不是正确的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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