简体   繁体   English

文件正在由另一个进程使用,但是canRead()返回true

[英]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.

相关问题 getResourceAsStream返回null,但File.canRead()返回true - getResourceAsStream returns null but File.canRead() returns true File.canRead()返回false - File.canRead() returns false file.delete()返回false,即使file.exists(),file.canRead(),file.canWrite(),file.canExecute()都返回true - file.delete() returns false even though file.exists(), file.canRead(), file.canWrite(), file.canExecute() all return true Android canRead()返回false - Android canRead() returns false file.canWrite(); file.canRead(); file.canExceute(); 虽然我的文件/目录没有访问权限,但总是返回true - file.canWrite(); file.canRead(); file.canExceute(); always return true though my file/directory had no access rights isDirectory() 为文件返回 true - isDirectory() returns true for a file Android / Java File.canRead()奇怪的问题 - Android/Java File.canRead() strange issue 如何确定另一个进程是否正在使用该文件(Java) - How to determine if file is in use by another process (Java) UserException:尝试打开另一个进程当前正在使用的odb数据库文件 - UserException: Attempt to open odb database file that is currently in use by another process Java.io.File.canRead()与Java.nio.Files.isReadable()与Java.io.FilePermission - Java.io.File.canRead() vs Java.nio.Files.isReadable() vs Java.io.FilePermission
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM