Java ClassLoader Load getFilesInPackage(String packageName)

Here you can find the source of getFilesInPackage(String packageName)

Description

Given a package name, attempts to reflect to find all file names within the package on the local file system.

License

Open Source License

Parameter

Parameter Description
packageName a parameter

Return

File names

Declaration

public static List<String> getFilesInPackage(String packageName) 

Method Source Code

//package com.java2s;
/* Copyright 2012 Yaqiang Wang,
* yaqiang.wang@gmail.com/* w  w  w. ja v a  2  s.  com*/
* 
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
* 
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
* General Public License for more details.
*/

import java.io.File;

import java.net.URL;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Given a package name, attempts to reflect to find all file names within the
     * package on the local file system.
     *
     * @param packageName
     * @return File names
     */
    public static List<String> getFilesInPackage(String packageName) {
        List<String> fns = new ArrayList<>();
        //String packageNameSlashed = "/" + packageName.replace(".", "/");
        String packageNameSlashed = packageName.replace(".", "/");
        // Get a File object for the package  
        URL directoryURL = Thread.currentThread().getContextClassLoader().getResource(packageNameSlashed);
        if (directoryURL == null) {
            System.out.println("Could not retrieve URL resource: " + packageNameSlashed);
            return fns;
        }

        String directoryString = directoryURL.getFile();
        if (directoryString == null) {
            System.out.println("Could not find directory for URL resource: " + packageNameSlashed);
            return fns;
        }

        File directory = new File(directoryString);
        if (directory.exists()) {
            // Get the list of the files contained in the package  
            String[] files = directory.list();
            for (String fileName : files) {
                fns.add(fileName);
            }
        } else {
            System.out.println(packageName + " does not appear to exist as a valid package on the file system.");
        }
        return fns;
    }
}

Related

  1. getFileContents(String filename)
  2. getFileContents(String path)
  3. getFileFromPath(Object obj, String fileName)
  4. getFileFromPool(String path)
  5. getFileFullPath(String file)
  6. getFullPathByPkg(String pkg)
  7. getImageFilename(String name)
  8. getInputStream(String name)
  9. getLibKoyaVersion()