Java File Name Get getFileNames(String path, boolean withFullPath)

Here you can find the source of getFileNames(String path, boolean withFullPath)

Description

Returns a string array of file names in a path directory, with or without the full path

License

Open Source License

Parameter

Parameter Description
path a parameter
withFullPath a parameter

Return

string array

Declaration

public static String[] getFileNames(String path, boolean withFullPath) 

Method Source Code


//package com.java2s;
/*/*w  w w  . jav  a 2 s . com*/
 * Copyright (c) 2012 Diamond Light Source Ltd.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

import java.io.File;
import java.io.FilenameFilter;

import java.util.Arrays;

public class Main {
    /**
     * Returns a string array of file names in a path directory, with or without
     * the full path
     * 
     * @param path
     * @param withFullPath
     * @return string array
     */
    public static String[] getFileNames(String path, boolean withFullPath) {
        File dir = new File(path);
        String[] children = dir.list();
        if (children == null) {
            return null;
        }
        // We filter any files that start with '.' or directory
        FilenameFilter filter = new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                File f = new File(dir.getAbsolutePath() + "/" + name);
                if (f.isDirectory())
                    return false;
                return !name.startsWith(".");
            }
        };
        children = dir.list(filter);
        Arrays.sort(children);
        if (withFullPath) {
            for (int i = 0; i < children.length; i++) {
                children[i] = path + "/" + children[i];
            }
        }
        return children;
    }
}

Related

  1. getFilenames(String dir, FilenameFilter filter)
  2. getFilenames(String dirctory)
  3. getFileNames(String directory, String file)
  4. getFileNames(String dirPath)
  5. getFilenames(String filenames)
  6. getFileNames_STR(String path)
  7. getFileNamesFromDir(File rootDir, String indexName)
  8. getFileNamesInJar(File source, String folder, String extension)
  9. getFileNamesOld(File zipFile)