日期:2014-05-20  浏览次数:20828 次

求助,java小程序
import java.io.*;
public class Test3 {
public static void main(String args[]) {
String separator = File.separator;
String filename1 = "myfile1.txt";
String filename2 = "myfile2.txt";
String filename3 = "myfile3.txt";
String filename4 = "myfile4.txt";
String directory1 = "A" +separator + "B";
String directory2 = "A" +separator + "C";
String directory3 = "A" +separator + "B" + separator + "D";
String directory4 = "A" +separator + "B" + separator + "D"+ separator + "E";
File f1 = new File(directory1,filename1);
File f2 = new File(directory2,filename2);
File f3 = new File(directory3,filename3);
File f4 = new File(directory4,filename4);
isExist(f1);
isExist(f2);
isExist(f3);
isExist(f4);
File f = new File("d:/java/FrequentlyUsedClassers/A");
printTree(f,0);

}

public static void isExist(File f) {
if(f.exists()) {
System.out.println("文件名" + f.getAbsolutePath());
System.out.println("文件大小" + f.length());
}else {
f.getParentFile().mkdirs();
try {
f.createNewFile();
}catch(IOException e) {
e.printStackTrace();
}
}
}

public static void printTree(File f,int level) {
String preStr = "";
for(int i=0; i<level; i++) {
preStr += " ";
}
File[] a = f.listFiles();
for(int i=0; i < a.length; i++) {
System.out.println(a[i].getName());
if(a[i].isDirectory()) {
printTree(a[i],level + 1);
}
}
}
}




报错nullpointerexception



------解决方案--------------------
Java code
for(int i=0; i < a.length; i++)

------解决方案--------------------
Java code
String directory1 = "A" +separator + "B";

------解决方案--------------------
Java code
String directory1 = "d:/java/FrequentlyUsedClassers/A" +separator + "B";
String directory2 = "d:/java/FrequentlyUsedClassers/A" +separator + "C";
String directory3 = "d:/java/FrequentlyUsedClassers/A" +separator + "B" + separator + "D";
String directory4 = "d:/java/FrequentlyUsedClassers/A" +separator + "B" + separator + "D"+ separator + "E";
File f1 = new File(directory1,filename1);
File f2 = new File(directory2,filename2);
File f3 = new File(directory3,filename3);
File f4 = new File(directory4,filename4);

------解决方案--------------------
Java code


package a;

import java.io.*;

public class Test3 {
    public static void main(String args[]) {
        String separator = File.separator;
        String basePath = "d:" + separator +"java"+ separator  ; //需要指明每个目录在那个磁盘上啊 
        String filename1 = "myfile1.txt";
        String filename2 = "myfile2.txt";
        String filename3 = "myfile3.txt";
        String filename4 = "myfile4.txt";
        String directory1 = basePath + "A" + separator + "B";
        String directory2 = basePath + "A" + separator + "C";
        String directory3 = basePath + "A" + separator + "B" + separator + "D";
        String directory4 = basePath + "A" + separator + "B" + separator + "D" + separator
                + "E";
        File f1 = new File(directory1, filename1);
        File f2 = new File(directory2, filename2);
        File f3 = new File(directory3, filename3);
        File f4 = new File(directory4, filename4);
        isExist(f1);
        isExist(f2);
        isExist(f3);
        isExist(f4);
        File f = new File(basePath);
//        isExist(f);
        printTree(f, 0);

    }

    public static void isExist(File f) {
        if(f.isDirectory() && !f.exists()){
            f.mkdirs() ; 
        }else{
            if (f.exists()) {
                System.out.print("文件名" + f.getAbsolutePath());
                System.out.println("   文件大小" + f.length());
            } else {
                    f.getParentFile().mkdirs();
                    try {
//                        System.out.println( "-------------"+f.getAbsolutePath())  ;
                        f.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }    
        }
    }

    public static void printTree(File f, int level) {
        String preStr = "";
        for (int i = 0; i < level; i++) {
            preStr += " ";
        }
        File[] a = f.listFiles();
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i].getName());
            if (a[i].isDirectory()) {
                printTree(a[i], level + 1);
            }
        }
    }
}