【发布时间】:2013-11-14 02:01:02
【问题描述】:
我正在开发一个循环特定目录(例如 C:\Documents)并计算它在磁盘上占用的总大小的程序。但是,由于某种原因,我的程序在似乎在 Documents 文件夹中查找名为“My Music”的文件夹时不断抛出空指针异常。 “我的音乐”在我的 Documents 文件夹中不存在,所以我很困惑它从哪里获取。我理解它为什么会抛出异常(显然,如果它在指定目录中找不到文件夹,它将返回 null),但我不知道它是如何找到“我的音乐”的。这是我的代码:
public static Long getDirSize(File directory) {
long size = 0L;
for (File file : directory.listFiles()) {
size += file.isDirectory() ? getDirSize(file) : file.length();
}
return size;
}
要调用此方法,我使用以下代码:
long required = 0;
for (int i = 0; i < directories.length; i++){
required = required + getDirSize(new File(directories[i]));
"directories" 是一个字符串数组,其中包含我要计算其大小的目录。例如, 目录 = {"C:\Users\user\Documents", "C:\用户\用户\图片", "C:\Users\user\Videos"}
我已经尝试解决这个问题好几个星期了,尝试不同的方法来遍历目录等,它们似乎都给了我同样的问题。我将非常感谢另一组对此的看法。谢谢!
编辑:这是堆栈跟踪--
java.lang.NullPointerException
at diana.Review.getDirSize(Review.java:206)
at diana.Review.getDirSize(Review.java:207)
at diana.Review$2.actionPerformed(Review.java:126)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
【问题讨论】:
-
代码看起来不错,你能发布你的堆栈跟踪吗?
-
让我猜猜。您使用的是 Windows Viste 还是更高版本?在这种情况下,MyDocuments 是一个 Junction(我认为它们仍然没有迁移到符号链接,尽管 NTFS 现在支持它们),而 Java 似乎无法处理 NTFS 文件系统上的那些。
-
@user2310289 Stacktrace 已添加。
-
@Johannes H,你说得对——我在 Windows 8 上。
-
@JohannesH.,在这种情况下,我之前没有听说过“Junction”这个词。在这种情况下是什么意思?