繁体   English   中英

如何在运行时杀死Java中的javaw.exe

[英]How to kill javaw.exe in java at runtime

我有一个生成输出文件的Java代码。 当我想在运行时使用此Java代码删除此输出文件时,无法将其删除。 例如:

File file2= new File("D:\\eclipse-jee-mars-R-win32-x86_64\\eclipse\\output.txt");
    if(file2.delete()){
                System.out.println(file2.getName() + " is deleted!");
            }
            else{
                System.out.println("Delete operation is failed.");
            }

此代码无效。 (“ output.txt”文件在此目录上生成,可以在文件夹中看到。)

事实上,当我想在Windows文件夹上删除时,它无法删除,并且会出现此错误。 我将翻译成英文。

屏幕截图

如果我在任务管理器中杀死了“ javaw.exe”进程,则可以在文件夹中删除该文件。 我的问题是,我该如何解决这个问题? 我想在Java运行时杀死“ javaw.exe”,以便可以删除此文件。 如何在Java运行时杀死此“ javaw.exe”进程? 如果在运行时在代码中杀死“ javaw.exe”进程,是否会遇到另一个问题?

如何生成此输出文件?

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.io.FileWriter;

public class code{

    public static void main(String[] args) {

        try (BufferedReader br = new BufferedReader(new FileReader(args[0])))
        {

            int lines = 0;
            while (br.readLine() != null) lines++; // to get text's number of lines 

            String sCurrentLine;
            BufferedReader br2 = new BufferedReader(new FileReader(args[0])); //to read and sort the text

            String[] array; //create a new array
            array = new String[lines];

            int i=0;
            while ((sCurrentLine = br2.readLine()) != null) {//fill array with text content
                array[i] = sCurrentLine;
                i++;
            }
            Arrays.sort(array); //sort array


            FileWriter fw = new FileWriter("output.txt");

            for (i = 0; i < array.length; i++) { //write content of the array to file
                fw.write(array[i] + "\n");
            }
            fw.close();


            System.out.println("Process is finished.");


        } catch (IOException e) {
            e.printStackTrace();
        } 

    }
}

实际上,我正在开发使用JSP在线编译/执行Java代码的Web应用程序。 我没有共享所有部分,因为如果共享代码的所有部分很难理解。 此代码生成一个输出文件,它接受输入文件,进行排序,然后创建一个新的“ output.txt”文件。 我想在我的JSP中删除此文件,但由于javaw.exe仍在运行,因此无法删除。

无论如何,输出文件都是在“ D:\\ eclipse-jee-mars-R-win32-x86_64 \\ eclipse”目录中生成的,我不想解释详细信息以防止思维混乱。

我解决了我的问题。 我忘了关闭我的BufferedReader。 我的程序中有一些过渡方法,我无法全部分享。 这些过渡方法是关于文件处理的,有一次,我忘记关闭创建了另一个位置的BufferedReader。 关闭BufferedReader之后,可以在其他地方动态删除“ output.txt”。 我正在分享我的viewtext方法。 它用于在jsp textarea中查看输出文件。

public static String viewtext(String pathname) throws IOException{


         String a="";

         File f = new File(pathname);
         if(f.exists() && !f.isDirectory()) { 
             BufferedReader br = new BufferedReader(new FileReader(pathname));
              for (String line; (line = br.readLine()) != null;) {
                  a = a  +  line + "\n"  ;
                  }
              br.close(); //I added just this 
             return a;
         }
         else{
            // br.close(); //No need here, if there is no file no close is needed.
             return "Your file is not found!";
     }

}

另外,也不需要杀死javaw.exe进程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM