0

我将如何扫描文件夹中的 name.class 文件,然后将它们加载到程序中的数组列表中。

我对我需要它做什么有一个大致的了解,我只是不知道我需要用什么来在代码中实现它。

扫描文件夹加载 .class 文件,使用 array.add(new class(params)); 将类添加到数组列表中 毕竟运行类中的方法。

这是我将模块(如果它们甚至被称为)加载到客户端的系统的当前方式

 package pro.skid.Gabooltheking.Module;

import java.util.ArrayList;

public class ModuleLoader {

    public static ArrayList<Module> module = new ArrayList<Module>();
    public final ArrayList<Module> getModule(){ return module; }

    public static void startModule(){
        module.clear();
    }

        public final Module getModuleByName(String moduleName){  
            for( Module module : getModule()){
                if(module.getName().equalsIgnoreCase(moduleName)){ return module; }
            }
            return null;
        }

        public static void keyBind(int key){ 
            for(Module m : module){
                if(m.getKey() == key){
                    m.toggle();
                }
            }
        }

        public static void runModuleTick(){
            for(Module m : module){
                if(m.getState()){
                    m.onTick();
                }
            }
        }

}

这就是抽象模块类的样子

package pro.skid.Gabooltheking.Module;

public abstract class Module {

    int key,color;
    String name;
    boolean state;

    /**
     * Set's the following variables
     * @param name- name of mod
     * @param key- keybind for mod
     * @param color- color in gui
     */
    public Module(String name, int key, int color){
        this.name = name;
        this.key = key;
        this.color = color;
    }
    /**
     * Set's the state of the mod to on or off.
     */
    public void toggle() 
    { state = !state; 
        if(this.getState())
            { 
             this.onToggle();
            }else{ 
             this.onDisable();
        } 
    }
    /**
     * Does something when mod is first toggled.
     * Does it only once.
     */
    public abstract void onToggle();

    /**
     * Does something when mod is disabled.
     * Does it only once.
     */
    public abstract void onDisable();

    /**
     * Does something when mod is toggled.
     * Loops untill hack is disabled.
     */
    public abstract void onTick();


    public String getName(){return this.name; }
    public int getKey(){ return this.key; }
    public int getColor(){ return this.color; }
    public boolean getState(){ return this.state; }
}

在我看来,所有的帮助都是很好的帮助。还要忽略那些让我更容易记住每种方法的作用的蹩脚评论。

4

1 回答 1

0

您可以使用 Apache commons IOUtils 和其中一个过滤器来递归地遍历目录并根据名称或扩展名匹配文件。

如果您不想要外部依赖项,那么标准库 ​​java.io.File 还包含几个您可以使用的列表方法。

于 2013-07-09T06:14:37.710 回答