Java Folder Read getFilesAsArrayList(String path)

Here you can find the source of getFilesAsArrayList(String path)

Description

get Files As Array List

License

Apache License

Declaration

public static ArrayList<String> getFilesAsArrayList(String path) 

Method Source Code


//package com.java2s;
/*//from   www.jav a2 s . c  o m
 * Copyright (C) 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import java.io.File;

import java.util.ArrayList;

public class Main {
    public static ArrayList<String> getFilesAsArrayList(String path) {
        ArrayList<String> mFileList = new ArrayList<String>();
        File root = new File(path);

        if (!storageReady()) {
            return null;
        }
        if (!root.exists()) {
            if (!createFolder(path)) {
                return null;
            }
        }
        if (root.isDirectory()) {
            File[] children = root.listFiles();
            for (File child : children) {
                String filename = child.getName();
                // no hidden files
                if (!filename.startsWith(".")) {
                    mFileList.add(child.getAbsolutePath());
                }
            }
        } else {
            String filename = root.getName();
            // no hidden files
            if (!filename.startsWith(".")) {
                mFileList.add(root.getAbsolutePath());
            }
        }
        return mFileList;
    }

    private static boolean storageReady() {
        return true;
        //        String cardstatus = Environment.getExternalStorageState();
        //        if (cardstatus.equals(Environment.MEDIA_REMOVED)
        //                || cardstatus.equals(Environment.MEDIA_UNMOUNTABLE)
        //                || cardstatus.equals(Environment.MEDIA_UNMOUNTED)
        //                || cardstatus.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
        //            return false;
        //        } else {
        //            return true;
        //        }
    }

    public static boolean createFolder(String path) {
        if (storageReady()) {
            boolean made = true;
            File dir = new File(path);
            if (!dir.exists()) {
                made = dir.mkdirs();
            }
            return made;
        } else {
            return false;
        }
    }
}

Related

  1. getFiles(String realpath, List files, String[] fileType)
  2. getFiles(String rootDirectory)
  3. getFiles(String thePaths[])
  4. getFilesAndFilesSubDirectories(String directoryName, String regexPattern)
  5. getFilesArray(File baseStorePath, final String fileNameSearchPattern)
  6. getFilesByDate(String directory, final boolean earliestFirst)
  7. getFilesByName(File path, String name)
  8. getFilesByPath(String toDir)
  9. getFilesByRegxFileName(String dir, final String regxName)