[英]File is in use by another process but canRead() returns true
I'm using another program to convert bin files to xml and then I'm trying to read the file. 我正在使用另一个程序将bin文件转换为xml,然后尝试读取该文件。 With the following code: 使用以下代码:
File file = new File(currentPath + "/ammunition.bin");
File file2 = fileTools.convert(file);
ArrayList<String> asd = fileTools.readFile(file2);
I get FileNotFoundException, "file is in use by another process". 我收到FileNotFoundException,“文件正在被另一个进程使用”。 What makes this weird is that file.canRead() returns true even though i get the exception. 奇怪的是,即使我得到了异常,file.canRead()也返回true。 Here is the rest of the code: 这是其余的代码:
public ArrayList<String> readFile(File file) {
ArrayList<String> result = new ArrayList<>();
Scanner reader;
try {
reader = new Scanner(file, "UTF-8");
} catch (FileNotFoundException ex) {
boolean b = file.canRead();
StringWriter errors = new StringWriter();
ex.printStackTrace(new PrintWriter(errors));
JOptionPane.showMessageDialog(null, errors.toString());
alertError("readFile\n" + b + " " + ex.getMessage());
return null;
}
while (reader.hasNext()) {
result.add(reader.nextLine());
}
reader.close();
return result;
}
public File changeFileExtension(File f, String ext) {
String name = f.getAbsolutePath();
int i = name.lastIndexOf(".");
name = name.substring(0, i) + "." + ext;
f = new File(name);
return f;
}
public File convert(File file) {
File binConverter = new File(this.currentPath + "\\GibbedsTools\\Gibbed.Avalanche.BinConvert.exe");
if (!binConverter.exists()) {
alertError("convert\nGibbedsTools is missing, it should be in\n" + this.currentPath + "\\GibbedsTools\\Gibbed.Avalanche.BinConvert.exe");
return null;
}
if (!file.exists()) {
alertError("convert\nFile does not exist\n" + file.getAbsolutePath());
}
String name = file.getName();
String extension = name.substring(name.lastIndexOf(".") + 1, name.length());
Runtime rt = Runtime.getRuntime();
try {
rt.exec("cmd.exe /c " + "\"\"" + this.currentPath + "\\GibbedsTools\\Gibbed.Avalanche.BinConvert.exe\" \"" + file.getAbsolutePath() + "\"\"");
} catch (IOException ex) {
alertError("binToXml\n" + ex.getMessage());
return null;
}
File file2 = changeFileExtension(file, extension.equals("xml") ? "bin" : "xml");
int timepassed = 0;
while (!file2.exists() || !file2.canWrite() || timepassed <= 50) {
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
alertError("binToXml\n" + ex.getMessage());
return null;
}
timepassed += 50;
if (timepassed > 3000) {
if (!file2.exists()) {
alertError("convert\nError, binconverter probably crashed (No xml file created after 3 seconds from passing file to binconverter)\n"
+ "xml file should be in:\n"
+ file2.getAbsolutePath() + " Found: " + file2.exists());
return null;
}
}
if (timepassed > 6000 && !file2.canWrite()) {
alertError("convert\nCan't write to the xml file after 6 seconds from passing the file to binconverter."
+ file2.getAbsolutePath() + " Can write: " + file2.canWrite());
return null;
}
}
return file2;
}
instead of having : 而不是:
Runtime rt = Runtime.getRuntime();
.
.
.
try this instead: 试试这个代替:
Process p = Runtime.getRuntime().exec("cmd.exe /c " + "\"\"" + this.currentPath + "\\GibbedsTools\\Gibbed.Avalanche.BinConvert.exe\" \"" + file.getAbsolutePath() + "\"\"");
then you can wait for it to finish by using: 那么您可以使用以下命令等待其完成:
p.waitFor();
once external program finishes it then releases the file. 外部程序完成后,将释放文件。 better solution would be using Processbuilder 更好的解决方案是使用Processbuilder
You can also destroy the process by using 您还可以使用以下方法破坏进程
p.destroy();
of course try to avoid using it. 当然要避免使用它。 unless program is pain to deal with. 除非程序很难处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.