KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bsh > commands > dir


1 /**
2     Display the contents of the current working directory.
3     The format is similar to the Unix ls -l
4     <em>This is an example of a bsh command written in Java for speed.</em>
5     
6     @method void dir( [ String dirname ] )
7 */

8 package bsh.commands;
9
10 import java.io.*;
11 import bsh.*;
12 import java.util.Date JavaDoc;
13 import java.util.Vector JavaDoc;
14 import java.util.GregorianCalendar JavaDoc;
15 import java.util.Calendar JavaDoc;
16
17 public class dir
18 {
19     static final String JavaDoc [] months = { "Jan", "Feb", "Mar", "Apr",
20         "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
21
22     public static String JavaDoc usage() {
23         return "usage: dir( String dir )\n dir()";
24     }
25
26     /**
27         Implement dir() command.
28     */

29     public static void invoke( Interpreter env, CallStack callstack )
30     {
31         String JavaDoc dir = ".";
32         invoke( env, callstack, dir );
33     }
34
35     /**
36         Implement dir( String directory ) command.
37     */

38     public static void invoke(
39         Interpreter env, CallStack callstack, String JavaDoc dir )
40     {
41         File file;
42         try {
43             file = env.pathToFile( dir );
44         } catch (IOException e ) {
45             env.println("error reading path: "+e);
46             return;
47         }
48
49         if ( !file.exists() || !file.canRead() ) {
50             env.println( "Can't read " + file );
51             return;
52         }
53         if ( !file.isDirectory() ) {
54             env.println("'"+dir+"' is not a directory");
55         }
56
57         String JavaDoc [] files = file.list();
58         files = StringUtil.bubbleSort(files);
59
60         for( int i=0; i< files.length; i++ ) {
61             File f = new File( dir + File.separator + files[i] );
62             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
63             sb.append( f.canRead() ? "r": "-" );
64             sb.append( f.canWrite() ? "w": "-" );
65             sb.append( "_" );
66             sb.append( " ");
67
68             Date JavaDoc d = new Date JavaDoc(f.lastModified());
69             GregorianCalendar JavaDoc c = new GregorianCalendar JavaDoc();
70             c.setTime(d);
71             int day = c.get(Calendar.DAY_OF_MONTH);
72             sb.append( months[ c.get(Calendar.MONTH) ] + " " + day );
73             if ( day < 10 )
74                 sb.append(" ");
75
76             sb.append(" ");
77
78             // hack to get fixed length 'length' field
79
int fieldlen = 8;
80             StringBuffer JavaDoc len = new StringBuffer JavaDoc();
81             for(int j=0; j<fieldlen; j++)
82                 len.append(" ");
83             len.insert(0, f.length());
84             len.setLength(fieldlen);
85             // hack to move the spaces to the front
86
int si = len.toString().indexOf(" ");
87             if ( si != -1 ) {
88                 String JavaDoc pad = len.toString().substring(si);
89                 len.setLength(si);
90                 len.insert(0, pad);
91             }
92             
93             sb.append( len.toString() );
94
95             sb.append( " " + f.getName() );
96             if ( f.isDirectory() )
97                 sb.append("/");
98
99             env.println( sb.toString() );
100         }
101     }
102 }
103
104
Popular Tags