新手提问 java 遍历目录所有文件

rpwudi123 2017-09-08 09:41:27

import java.io.*;

class FileDemo {
public static void main(String[] args) {
File dir = new File("c:\\");
showDir(dir);

}

public static void showDir(File dir) {

File[] files = dir.listFiles();
for(int x = 0 ; x < files.length ; x++) {
if(files[x].isDirectory()) {
showDir(files[x]);
}
else
System.out.println(files[x]);
}


}
}

为何报空指针啊,代码我是照着教程打的,教程并没有报错呢。


...全文
424 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
蓝以雪 2017-09-12
  • 打赏
  • 举报
回复
你直接判断它是文件就打印,不是就递归,干嘛要判断是不是文件夹,文件夹里面还有空的
mmqw 2017-09-12
  • 打赏
  • 举报
回复
换成d盘试试,先自己创建一个文件夹,里边放点文件试试
galiniur0u 2017-09-12
  • 打赏
  • 举报
回复
你这访问C盘当然会有问题,权限不足啊。
Timor_D 2017-09-11
  • 打赏
  • 举报
回复
c盘有很多文件是不可读的
「已注销」 2017-09-11
  • 打赏
  • 举报
回复
Windows 系统有很多目录是链接,指向另外一个目录,或者没有权限访问的,你自己创建一个去看看就知道了没问题。
rickylin86 2017-09-09
  • 打赏
  • 举报
回复
public File[] listFiles() Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname. If this abstract pathname does not denote a directory, then this method returns null. Otherwise an array of File objects is returned, one for each file or directory in the directory. Pathnames denoting the directory itself and the directory's parent directory are not included in the result. Each resulting abstract pathname is constructed from this abstract pathname using the File(File, String) constructor. Therefore if this pathname is absolute then each resulting pathname is absolute; if this pathname is relative then each resulting pathname will be relative to the same directory. There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order. Note that the Files class defines the newDirectoryStream method to open a directory and iterate over the names of the files in the directory. This may use less resources when working with very large directories. Returns: An array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs. Throws: SecurityException - If a security manager exists and its SecurityManager.checkRead(String) method denies read access to the directory Since: 1.2 这是API里面的说明
rpwudi123 2017-09-09
  • 打赏
  • 举报
回复

import java.io.*;

class FileDemo {
	public static void main(String[] args) {
		File dir = new File("c:\\");
		showDir(dir);

	}

	public static void showDir(File dir) {

		File[] files = dir.listFiles();
		// System.out.println(files == null);
		if (files != null) {
			for (int x = 0; x < files.length; x++) {

				if (files[x].isDirectory())
					showDir(files[x]);

				else
					System.out.println(files[x]);

			}
		}

	}
}

问题解决了,但为什么有时候files会返回空呢,我测试过传空目录进去也没有返回null。
zxjuanjuan 2017-09-09
  • 打赏
  • 举报
回复
你得对files 判断是否为空
rpwudi123 2017-09-08
  • 打赏
  • 举报
回复
话说如何修改自己的回复,上面图片没显示出来,少了一个中括号。。。
rpwudi123 2017-09-08
  • 打赏
  • 举报
回复
[img=https://img-bbs.csdn.net/upload/201709/08/1504881407_633808.jpg][/img
目录换成C:\Program Files后如图显示,没有直接报错,但还是报错了。
换成子目录比较少的目录后,不报错。

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧