Reputation: 2445
I'm using another program to convert bin files to xml and then I'm trying to read the file. 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". What makes this weird is that file.canRead() returns true even though i get the exception. 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;
}
Upvotes: 1
Views: 204
Reputation: 5423
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
You can also destroy the process by using
p.destroy();
of course try to avoid using it. unless program is pain to deal with.
Upvotes: 1