listingservice.java

来自「a flex project similar a "window eplorer」· Java 代码 · 共 96 行

JAVA
96
字号
package open;

import java.util.ArrayList;
import java.io.File;

// Service adapter that provides system-level file data
public class ListingService {
    // Default constructor
    public ListingService() {;}

    // Get a list of directories (only) based on a provided path
    public ArrayList directories( String path ) {
        ArrayList   dirs = new ArrayList();
        File        current = new File( path );
        File[]      result = current.listFiles();

        for( int d = 0; d < result.length; d++ ) {
            if( result[d].isDirectory() ) {
                dirs.add( new ServiceFile( result[d].getName(), result[d].getAbsolutePath(), false, hasChildren( result[d] ) ) );
            }
        }

        return dirs;
    }

    // Check a specified path for child structures
    // Use a provided path and return as quickly as possible based on presence
    private boolean hasChildren( File start ) {
        File[]      children = start.listFiles();
        boolean     contains = false;

        if( children != null ) {
            for( int c = 0; c < children.length; c++ ) {
                if( children[c].isDirectory() ) {
                    contains = true;
                    break;
                }
            }
        }

        return contains;
    }

    // Get a list of files (only) based on a provided path
    // Not used in current implementation of application
    public ArrayList files( String path ) {
        ArrayList   files = new ArrayList();
        File        current = new File( path );
        File[]      result = current.listFiles();

        for( int f = 0; f < result.length; f++ ) {
            if( result[f].isFile() ) {
                files.add( new ServiceFile( result[f].getName(), result[f].getAbsolutePath(), true ) );
            }
        }

        return files;
    }

    // Get a list of files and directories based on a provided path
    // To be Windows-esque, group directories first, then files
    public ArrayList list( String path ) {
        ArrayList   files = new ArrayList();
        File        current = new File( path );
        File[]      result = current.listFiles();

        for( int d = 0; d < result.length; d++ ) {
            if( result[d].isDirectory() ) {
                files.add( new ServiceFile( result[d].getName(), result[d].getAbsolutePath(), false, result[d].length(), result[d].lastModified() ) );
            }
        }

        for( int f = 0; f < result.length; f++ ) {
            if( result[f].isFile() ) {
                files.add( new ServiceFile( result[f].getName(), result[f].getAbsolutePath(), true, result[f].length(), result[f].lastModified() ) );
            }
        }

        return files;
    }

    // Mechanism for internal testing of the service
    public static void main( String[] args ) {
        ArrayList       files = null;
        ListingService  service = new ListingService();
        ServiceFile     current = null;

        files = service.directories( "C:\\" );

        for( int f = 0; f < files.size(); f++ ) {
            current = ( ServiceFile )files.get( f );
            System.out.println( "ServiceFile: " + current.getPath() );
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?