Listing 12: ListFiles.java — Lists directory entries with full information

import java.io.*;
import java.util.*;
import java.text.*;

class ListFiles
{
    public static void main(String[] args) throws IOException
    {
        listRoots();

        // Print current directory name:
        String curDir = System.getProperty("user.dir");
        File dir = new File(curDir);
        System.out.println(dir.getCanonicalPath() + ":");
        System.out.println("\trelative path: " + dir.getPath());
        System.out.println("\tabsolute path: "
                           + dir.getAbsolutePath());
        System.out.println("\tas URL: " + dir.toURL());
        System.out.println("==========");
        
        // List files:
        File[] files = dir.listFiles();
        SimpleDateFormat dateFormat =
            new SimpleDateFormat("MM-dd-yyyy kk:mm:ss");
        DecimalFormat sizeFormat =
            new DecimalFormat("########");
        for (int i = 0; i < files.length; ++i)
        {
            String name = buildColumn(files[i].getName(), 20);
            System.out.print(name + " ");

            String size =
                buildColumn(
                    sizeFormat.format(files[i].length()), 8
                           );
            System.out.print(size + " ");
                
            Date when = new Date(files[i].lastModified());
            System.out.print(dateFormat.format(when) + " ");
            if (files[i].isDirectory())
                System.out.print("d");
            if (files[i].canRead())
                System.out.print("r");
            if (files[i].canWrite())
                System.out.print("w");
            if (files[i].isHidden())
                System.out.print("h");
            System.out.println();
        }
    }

    static void listRoots()
    {
        File[] roots = File.listRoots();
        System.out.println("Roots on system:");
        for (int i = 0; i < roots.length; ++i)
            System.out.println("\t"+roots[i].getAbsolutePath());
        System.out.println();
    }        
    
    static String buildColumn(String s, int len)
    {
        // Force a string into a fixed-size column:
        // ... implementation omitted
    }
}

/* Output:
Roots on system:
        C:\
        D:\

C:\CUJ:
        relative path: C:\CUJ
        absolute path: C:\CUJ
        as URL: file:/C:/CUJ/
==========
Compare.java         414      11-22-2000 11:47:39 rw
Employee.java        2308     11-21-2000 15:51:27 rw
employees.dat        128      11-18-2000 17:59:28 rw
FileViewer.java      1830     11-18-2000 18:03:21 rw
FindFile.java        962      11-21-2000 15:50:08 rw
ListAllFiles.java    1310     11-22-2000 12:04:11 rw
ListFiles.class      2504     11-22-2000 12:10:51 rw
ListFiles.java       2631     11-21-2000 15:47:07 rw
ListSomeFiles.java   1400     11-22-2000 11:26:44 rw
ListZip.java         470      11-18-2000 23:48:01 rw
LogFile.java         578      11-21-2000 15:14:11 rw
logfile1.txt         44       11-21-2000 15:15:55 rw
LogFileTest.java     471      11-21-2000 15:37:27 rw
ProcessRecords.java  1341     11-21-2000 16:05:52 rw
PropTest.java        990      11-22-2000 11:07:15 rw
records.c            1059     11-21-2000 15:54:37 rw
Stack.java           463      11-08-2000 12:00:04 rw
temp                 0        11-22-2000 12:01:27 drw
Test.java            947      11-20-2000 18:36:01 rw
ViewFile.java        1538     11-21-2000 15:55:47 rw
*/
— End of Listing —