我有一个生成输出文件的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;我