查找文件空指针异常-各位大神帮忙看一下 谢谢

老男人1543 2016-11-25 05:16:14
错的地方我都标记出来了,各位帮帮看一眼,这个是因为什么导致的,还请详细的说一下,再次言谢~~!~~
public class Demo {
public static void main(String[] args) {
File file = new File("E:\\");
selectFile(file);//空指针异常
}

public static void selectFile(File file){
File [] files = file.listFiles();
List<File> list = new ArrayList<File>();
for(File f :files){//空指针异常
if(f.isFile()){
list.add(f);
}else if(f.isDirectory()){
selectFile(f);//空指针异常
}
}
for(File fileList:list){
if(".rar".endsWith(fileList.getName())){
System.out.println(fileList.getName());
}
}
}
}
...全文
376 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zss980909 2019-09-19
  • 打赏
  • 举报
回复
你可以直接用ishidden判断是否为隐藏文件 是就忽略 然后就不会报错了
zss980909 2019-09-19
  • 打赏
  • 举报
回复
是因为你没有权限去遍历 返回了null 所以才会空指针异常
  • 打赏
  • 举报
回复
引用 2 楼 oldman1543 的回复:
[quote=引用 1 楼 syy19940213 的回复:] file.listFiles(); 返回值是有可能为空的。 /** * Returns an array of abstract pathnames denoting the files in the * directory denoted by this abstract pathname. * * <p> If this abstract pathname does not denote a directory, then this * method returns {@code null}. Otherwise an array of {@code 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 {@link #File(File, * String) 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. * * <p> 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. * * <p> Note that the {@link java.nio.file.Files} class defines the {@link * java.nio.file.Files#newDirectoryStream(Path) 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. * * @return 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 * {@code 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 {@link * SecurityManager#checkRead(String)} method denies read access to * the directory * * @since 1.2 */ public File[] listFiles() { String[] ss = list(); if (ss == null) return null; 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; }
我把他的目录在添加一层,就没空指针了放到E://就会报空指针,为什么呢?[/quote]检查你的目录结构,你的E盘有可能再在某个目录的时候返回为空了
ichavin 2016-11-29
  • 打赏
  • 举报
回复
因为你这里面有一个递归,然而file.listFiles()这个方法是可能范围null的,当用foreach对null进行遍历的时候会报错
自由自在_Yu 2016-11-29
  • 打赏
  • 举报
回复
public class Test1 {
	public static void main(String[] args) {
		File file = new File("F:/");
		selectFile(file);// 空指针异常
	}

	public static void selectFile(File file) {
		File[] files = file.listFiles();
		List<File> list = new ArrayList<File>();
		for (File f : files) {// 空指针异常
			if(!(f.toString().contains("$RECYCLE.BIN"))){
				//每个磁盘都会有$RECYCLE.BIN这个回收站文件夹,这个文件夹读取不到,所以要排除这个文件夹
				if (f.isFile()) {
					list.add(f);
				} else if (f.isDirectory()) {
					selectFile(f);// 空指针异常
				}
			}			
		}
		for (File fileList : list) {
			if (fileList.getName().endsWith(".rar")) {	
				//这里endsWith写反了
				System.out.println(fileList.getName());
			}
		}
	}
}
希望我回答的是对的
Be_nurturing 2016-11-29
  • 打赏
  • 举报
回复
你这个没有异常啊,我试过了,没有异常啊
老男人1543 2016-11-28
  • 打赏
  • 举报
回复
引用 1 楼 syy19940213 的回复:
file.listFiles(); 返回值是有可能为空的。 /** * Returns an array of abstract pathnames denoting the files in the * directory denoted by this abstract pathname. * * <p> If this abstract pathname does not denote a directory, then this * method returns {@code null}. Otherwise an array of {@code 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 {@link #File(File, * String) 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. * * <p> 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. * * <p> Note that the {@link java.nio.file.Files} class defines the {@link * java.nio.file.Files#newDirectoryStream(Path) 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. * * @return 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 * {@code 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 {@link * SecurityManager#checkRead(String)} method denies read access to * the directory * * @since 1.2 */ public File[] listFiles() { String[] ss = list(); if (ss == null) return null; 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; }
我把他的目录在添加一层,就没空指针了放到E://就会报空指针,为什么呢?
  • 打赏
  • 举报
回复
file.listFiles(); 返回值是有可能为空的。 /** * Returns an array of abstract pathnames denoting the files in the * directory denoted by this abstract pathname. * * <p> If this abstract pathname does not denote a directory, then this * method returns {@code null}. Otherwise an array of {@code 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 {@link #File(File, * String) 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. * * <p> 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. * * <p> Note that the {@link java.nio.file.Files} class defines the {@link * java.nio.file.Files#newDirectoryStream(Path) 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. * * @return 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 * {@code 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 {@link * SecurityManager#checkRead(String)} method denies read access to * the directory * * @since 1.2 */ public File[] listFiles() { String[] ss = list(); if (ss == null) return null; 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; }

62,614

社区成员

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

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