我有一個生成輸出文件的java代碼。當我想在運行時刪除此java代碼中的輸出文件時,它不能被刪除。例如:如何在運行時殺死javaw中的javaw.exe
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」目錄上生成,我不想解釋細節以防止頭腦混淆。
您的代碼如何生成此輸出文件? – Berger
也許你必須在程序結束時使用System.exit(int i)?! – mrbela
好問題,通過使用 FileWriter fw = new FileWriter(「output.txt」); \t \t \t爲(I = 0;我