集册 Java实例教程 通过文件获取类名

通过文件获取类名

欢马劈雪     最近更新时间:2020-01-02 10:19:05

437
通过文件获取类名

/*
N  o w  J a v a . c o m 提供
*/

//package com.nowjava;

import java.io.File;


import java.util.ArrayList;

import java.util.List;


public class Main {


    private static List<String> getClassNameByFile(String filePath,

            List<String> className, boolean childPackage,

            String originalPackage) {

        List<String> myClassName = new ArrayList<String>();

        File file = new File(filePath);


        File[] childFiles = file.listFiles();

        for (File childFile : childFiles) {

            if (childFile.isDirectory()) {

                if (childPackage) {

                    myClassName.addAll(getClassNameByFile(

                            childFile.getPath(), myClassName, childPackage,/** nowjava - 时  代  Java 提供 **/

                            originalPackage));

                }

            } else {

                String childFilePath = childFile.getPath();

                if (childFilePath.endsWith(".class")) {

                    childFilePath = childFilePath.substring(

                            childFilePath.indexOf(originalPackage),

                            childFilePath.lastIndexOf(
展开阅读全文