Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

import java.*: File Processing


February 2001 Java Solutions/import java.*/Listing 12

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 —

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.