hi all:
import java.awt.*;
import java.io.*;
import java.awt.event.*;
public class Lister extends Frame{
TextArea ta;
public static void main(
String args[]){
String path=""; //******
if(args.length>=1)
path=args[0];
File f=new File(path);
if(!f.isDirectory()){
System.out.println(path+" doesn't exist or not dir");
System.exit(0);
}
Lister lister=new Lister(f);
lister.setVisible(true);
lister.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
Lister(File f){
setSize(300,450);
ta=new TextArea();
ta.setFont(new Font("Monospaced",Font.BOLD,14));
add(ta,BorderLayout.CENTER);
recurse(f,0);
}
void recurse(File dirfile,int depth){
String contents[]=dirfile.list();
for(int i=0;i<contents.length;i++){>
for(int spaces=0;spaces<depth;spaces++)>
ta.append(" ");
ta.append(contents[i]+"\n");
File child=new File(dirfile,contents[i]);
if(child.isDirectory())
recurse(child,depth+1);
}
}
}
In the definition of File(String pathname),an empty string as argument results in an abstract pathname for the current directory.
String path=" "; //add some space,it is an empty string (1)
and ,
String path=""; //no space,is also an empty string (2)
when (1) in the code(at //****** position),the output is all directories and files in the current directory,
but when I replaced (1) by (2), f.isDirectory() becomes false,so the output is
" doesn't exist or not dir".
I don't know why.Pls help me.