[英]How to kill javaw.exe in java at runtime
I have a java code that generates output file. 我有一个生成输出文件的Java代码。 When I want to delete this output file at runtime in this java code, it can not be deleted.
当我想在运行时使用此Java代码删除此输出文件时,无法将其删除。 For example:
例如:
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.");
}
This code doesn't work. 此代码无效。 ("output.txt" file is generated on this directory and can be seen in folder.)
(“ output.txt”文件在此目录上生成,可以在文件夹中看到。)
In fact When I want to delete on the windows folder it can not delete and it gives this error. 事实上,当我想在Windows文件夹上删除时,它无法删除,并且会出现此错误。 I will translate into English.
我将翻译成英文。
If I kill "javaw.exe" process in the task manager, I can delete this file on the folder. 如果我在任务管理器中杀死了“ javaw.exe”进程,则可以在文件夹中删除该文件。 My question is that, How can I solve this problem?
我的问题是,我该如何解决这个问题? I want to kill "javaw.exe" at runtime in java so this file can be deleted.
我想在Java运行时杀死“ javaw.exe”,以便可以删除此文件。 How to kill this "javaw.exe" process at runtime in java?
如何在Java运行时杀死此“ javaw.exe”进程? If I kill "javaw.exe" process at runtime in the code, will I encounter another problem?
如果在运行时在代码中杀死“ javaw.exe”进程,是否会遇到另一个问题?
How I generate this output file? 如何生成此输出文件?
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();
}
}
}
In fact, I am developing web application that compile/execute java codes online by using JSP. 实际上,我正在开发使用JSP在线编译/执行Java代码的Web应用程序。 I didn't share all parts because it is hard to understand if I share all parts of my code.
我没有共享所有部分,因为如果共享代码的所有部分很难理解。 This code generates an output file, it takes input file, sorts then crate a new "output.txt" file.
此代码生成一个输出文件,它接受输入文件,进行排序,然后创建一个新的“ output.txt”文件。 I want to in my JSP delete this file but It couldn't be deleted because javaw.exe is still running.
我想在我的JSP中删除此文件,但由于javaw.exe仍在运行,因此无法删除。
The output file is generated on the "D:\\eclipse-jee-mars-R-win32-x86_64\\eclipse" directory anyway, I don't want to explain details to prevent mind confusion. 无论如何,输出文件都是在“ D:\\ eclipse-jee-mars-R-win32-x86_64 \\ eclipse”目录中生成的,我不想解释详细信息以防止思维混乱。
I solved my problem. 我解决了我的问题。 I forgot to close my BufferedReader.
我忘了关闭我的BufferedReader。 There are some transition methods in my program I couldn't share all of them.
我的程序中有一些过渡方法,我无法全部分享。 These transition methods are about file handling and at one point, I forget to close BufferedReader that I created another location.
这些过渡方法是关于文件处理的,有一次,我忘记关闭创建了另一个位置的BufferedReader。 After I closed my BufferedReader, I could dynamically delete "output.txt" at someplace else.
关闭BufferedReader之后,可以在其他地方动态删除“ output.txt”。 I am sharing my viewtext method.
我正在分享我的viewtext方法。 It is used to view output file in jsp textarea.
它用于在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!";
}
}
Also there is no need to kill javaw.exe process. 另外,也不需要杀死javaw.exe进程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.