使用递归检索Windows分区的内容 [英] Retrieving contents of a windows partition using recursion

查看:66
本文介绍了使用递归检索Windows分区的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我已经编写了一个代码,用于使用Java中的递归来检索Windows分区"D:\"的内容.谁能帮我这个为什么没有运行.当我在代码中涉及到该注释语句时,显示了一个

 NullPointerException 

.



  import  java.io. *;
课程
{

静态 公共  void  getFolders(字符串驱动器)
{
尝试
{
File a =新文件(驱动器);
文件b [] = a.listFiles();
 for ( int  i = 1; i 如果(b [i] .isDirectory()== true)
{
字符串 str = b [i] .getName().toString();
System.out.println(str);
//  getFolders(str); 
}
}
}
捕获(异常er)
{
System.out.println(er);
}
}


公共 静态  void  main(字符串 args [])
{
u.getFolders(" );
}
} 



谢谢,
Akky

解决方案

好,这里有3个问题,然后您可能想添加1件事,正如Richard MacCutchan所说的(检查b是否为空)

1-您需要使用d:\\而不是d:
来调用getFolders() 2-您的迭代循环将需要从0索引而不是1开始
3-如果您想递归工作,请使用绝对路径

这是适合您的代码.

PS:抛出空指针异常是因为您不是通过使用目录的整个路径而是通过目录的名称来调用"getFolders"方法.因此,您传递的是"Directoryname",而不是"d:\ DirectoryName",如果您在其上调用"listFiles()",它将返回null.

 静态 公共  void  getFolders(字符串驱动器){
    尝试 {

        归档a =  File(驱动器);
        文件b [] = a.listFiles();
        // /从索引0而不是1开始循环
         for ( int  i =  0 ; i < b.length; i ++){
            如果(b [i] .isDirectory()== true){
                // /使用绝对路径启用递归" 
                字符串 str = b [i] .getAbsolutePath().toString();
                System.out.println(str);
                getFolders(str);
            }
        }
    } 捕获(异常er){
        System.out.println(er);
    }
}

公共 静态  void  main(字符串 [] args){
    // 用名称中的斜杠调用方法
    getFolders(" );
}


我删除了无效答案.


尝试更改代码以测试b的空值,从而:

文件b [] = a.listFiles();
如果(b == null)
{
System.out.println(" );
返回;
} 


您还将注意到,路径(在第1阶段)不包含驱动器号前缀,因此当您递归到目录时,尝试搜索无效路径名,该路径名不会返回任何匹配.


Hi everyone,
I have written a code for retrieving contents of a windows partition "D:\" using recursion in java. Can anybody help me that why this from is not running. There is a

NullPointerException

shown as I involve that comment statement in code.



import java.io.*;
class u
{

static public void getFolders(String drive)
{
	try
	{
File a=new File(drive);
File b[]=a.listFiles();
	for(int i=1;i<b.length;i++)
	{
	if(b[i].isDirectory()==true)
	{
String str=b[i].getName().toString();
System.out.println(str);
//getFolders(str);
	}	
	}
	}
	catch(Exception er)
	{
	System.out.println(er);
	}
}


public static void main(String args[])
{
u.getFolders("d:");
}
}



Thanks,
Akky

解决方案

Ok, there are 3 problems here and then 1 thing you probably want to add as Richard MacCutchan said (Checking if b is null)

1 - You need to call getFolders() using d:\\ not d:
2 - Your iterative loop will need to start from the 0 index not 1
3 - Use absolute pathing if you want recursion to work

Here is the code which should work for you.

PS: The null pointer exception is being thrown because you were calling the "getFolders" method by not using the entire path of the directory, but rather the name of the directory. So instead of ''d:\DirectoryName'' you were passing in ''Directoryname'', which would return null if you call "listFiles()" on it

static public void getFolders(String drive) {
    try {

        File a = new File(drive);
        File b[] = a.listFiles();
        /// Start your loop from index 0 not 1
        for (int i = 0; i < b.length; i++) {
            if (b[i].isDirectory() == true) {
                /// Use absolute pathing to enable "recursiveness"
                String str = b[i].getAbsolutePath().toString();
                System.out.println(str);
                getFolders(str);
            }
        }
    } catch (Exception er) {
        System.out.println(er);
    }
}

public static void main(String[] args) {
    // Call the method with a slash in the name
    getFolders("d:\\");
}


Non valid answer removed by me.


Try changing your code to test for null values of b thus:

File b[]=a.listFiles();
if (b == null)
{
	System.out.println("b is null"); 
	return;
}


You will also note that your paths (in phase 1) do not include the drive letter prefix so when you recurse into a directory you try a search on an invalid path name which returns no hits.


这篇关于使用递归检索Windows分区的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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