• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I feel confused!

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic