在源代码中调用.class文件?

时间:2016-10-26 21:09:29

标签: java

我一直在研究一个程序,我遇到了无数难以解决的问题。幸运的是我每次都得到了帮助。但是这个很混乱,我不确定如何正确描述它。

基本上我拥有的是一个主要的源代码,这是人们将要运行的,以及创建界面的内容。我有另一个源代码,它是从指定目录中删除文件的。

我需要做的是以某种方式调用删除文件的代码。我需要在主要源代码中调用它。

以下是我正在使用的if语句之一:

if(chckbxTemporaryFilesUser.isSelected()) {

}

如您所见,if语句检查是否选中了复选框。现在我想要做的是,如果返回true,则以某种方式激活删除代码。

我基本上做的是,就像一个简单的清洁软件。单击一个名为“临时文件”的复选框,如果选中它,另一个代码将删除“临时文件”文件夹中的所有文件。我通过Action Listener将所有if语句连接到JButton。因此,只有在按下按钮时才会执行代码。

我尝试在if语句中复制并粘贴整个代码。但无论我如何更改它,无论是删除类或方法,还是以某种方式更改它们,它总是会出现大量错误。根据我的经验,这意味着我不应该这样做。

以下是删除临时文件的代码:

public class TempFiles {
private static final
    String SRC_FOLDER = System.getProperty("user.home") + "\\AppData\\Local\\Temp";

public static void main(String[] args) {
    File directory = new File(SRC_FOLDER);

    //Check if directory exists
    if(!directory.exists()) {
       System.out.println("Directory does not exist.");
       System.out.println("Skipping directory.");
       System.exit(0);
    }
    else {

       try {
       delete(directory);
       }

       catch(IOException e){
           e.printStackTrace();
           System.exit(0);
       }
    }

    System.out.println("Cleaned directory " + SRC_FOLDER + ".");
}

public static void delete(File file)
    throws IOException{

    if(file.isDirectory()){

        //If directory is empty
        if(file.list().length==0){

        }
    else {

           //List all the directory contents
           String files[] = file.list();

           for (String temp : files) {
              //Construct the file structure
              File fileDelete = new File(file, temp);

              //Recursive delete
             delete(fileDelete);
           }

           //Check the directory again
           if(file.list().length==0) {
           }
        }

    }
  else {
        //If file exists, then delete it
        file.delete();
        System.out.println("File is deleted : " + file.getAbsolutePath());
    }
  }
}

基本上我要问的是,如何使用另一段代码运行一段代码?

如果他们确实涉及大量代码,请尝试用简单的术语解释你的答案,因为我仍然是Java的新手,尽管现在至少工作了一个月。我环顾四周,发现了一些类似的问题,但所有的答案和建议对我而言都太技术化了。

P.S。就像问题的标题也应该表明的那样,这两个代码在不同的文件中。

1 个答案:

答案 0 :(得分:2)

首先,没有什么能阻止你调用另一个类的main方法:

if(chckbxTemporaryFilesUser.isSelected()) {
    TempFiles.main(new String[0]);
}

但是有两个问题:

  1. 这不是通常的方式,所以它会使读者感到困惑。
  2. System.exit()TempFiles.main()的调用也会导致调用程序终止。我认为这不是你想要的。
  3. 对于第一个项目,我更愿意将删除临时文件的代码删除到仍在TempFiles类中的单独静态方法中:

    public static void deleteTemporaryFiles() {
        File directory = new File(SRC_FOLDER);
    
        // Check if directory exists
    
        // ... etc. ...
    
        System.out.println("Cleaned directory " + SRC_FOLDER + ".");
    }
    

    TempFiles.main()和复选框上的if语句中调用此新方法。来自main()

    public static void main(String[] args) {
        deleteTemporaryFiles();
    }
    

    来自你的if语句:

        if (chckbxTemporaryFilesUser.isSelected()) {
            TempFiles.deleteTemporaryFiles();
        }
    

    对于第2项,您需要用

    替换只读System.exit(0);的行
            return;
    

    如果您需要进一步澄清,请在评论中询问。