有没有办法检查文件夹中的所有类文件,并为文件夹中存在的每个文件创建一个新对象?对于java ...
答案 0 :(得分:1)
以下是从目录调用类的示例。
public class InvokeClass {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
try {
String classDerectory = "C:\\classes";
File folder = new File(classDerectory);
// read derectory
for (final File fileEntry : folder.listFiles()) {
// URLClassLoader so convert file to url
URLClassLoader classLoader = new URLClassLoader(new URL[]{fileEntry.toURI().toURL()});
//get class from loader
Class<?> clazz = classLoader.loadClass(fileEntry.getName());
// get new instance
Object obj=clazz.newInstance();
// do something with object.......
}
} catch (Exception e) {
// something went wrong..
e.printStackTrace();
}
}
}