Here you can find the source of getFilesInPackage(String packageName)
Parameter | Description |
---|---|
packageName | a parameter |
public static List<String> getFilesInPackage(String packageName)
//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; } }