簡體   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