检查文件是否在文件夹中,并要创建该文件的实例(如果存在)

所以我想做的是

  1. 接受用户输入(应该是文件名),并检查该文件是否存在于我当前的工作文件夹中。 (我不需要帮助,这已受到控制)
  2. 如果文件存在,我想创建该文件的实例。 (我想这样做,以便可以使用反射并检查文件是否为特定接口,并检查构造函数。

因此,为了测试我的程序,我创建了一个现有文件的实例,如下所示:

File tempFile = new File("/CURRENTFOLDER/" + file + ".java");
//with the user input tempFile = Test1.


    if(tempFile.exists()){

        Test1 test1 = new Test1();
        Class<?> test1_class = test1.getclass();

但是我真正想做的是这样的:

   File tempFile = new File("CURRENTFOLDER" + file + ".java");

    if(tempFile.exists()){

        //Test1 test1 = new Test1();
        //Class<?> test1_class = test1.getclass();

        tempFile instance_of_tempFile = new tempFile();
        Class<?> ? test_class = instance_of_tempFile.getclass();

我知道,您不会写tempFile instance_of_tempFile = new tempFile();,而只写了那封信,这样您就可以理解我的工作。

chen20060450 回答:检查文件是否在文件夹中,并要创建该文件的实例(如果存在)

实际上,可以使用ClassLoader

基本上,您必须使用所需的功能来实现类加载器,然后才能通过文件实例化类。

这里是brief how-to

example也可能有用

本文链接:https://www.f2er.com/3138798.html

大家都在问