You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XML读取失败后移动文件触发FileSystemException问题求助

问题:XML解析失败后无法移动文件,提示文件被占用

开发XML文件读取功能时,正常文件读取成功后可顺利移至success文件夹,但读取错误文件失败后,执行移动操作抛出java.nio.file.FileSystemException,错误提示:

java.nio.file.FileSystemException: c:\inbox\TEST.XML -> c:\error\TEST.XML: The process cannot access the file because it is being used by another process.

相关代码如下:

private void start(){
    //Some stuff
    boolean ok = true;
    try{
         readXml(file.getPath());
    } catch(XmlParserException xmle){
        log.error("Error");
        ok = false;
    } finally {
        moveFiles(ok, file);
    }
}

private void moveFiles(boolean parseOk, File file){
    //Set path with success or error folder
    String path = parseoOk ? (String) mainProperties.get(Constants.SUCCESS)
            : (String) mainProperties.get(Constants.ERROR);

    File fileTo = new File(path + Constants.ROUTE_SLASH + file.getName());

    try {
        Files.move(file.toPath(), fileTo .toPath(), StandardCopyOption.REPLACE_EXISTING);
    } catch (FileSystemException fse) {
        log.error("In Use");
        log.error(fse.getMessage());
    } catch (IOException e) {
        log.error("IOERROR");
        log.error(e.getMessage());
    }
}

private void readXml(String rutaFichero) throws XmlParserException{
    DocumentBuilder documentBuilder = initializeDocumentBuilder();
    try {
        File archivo = new File(rutaFichero);
        Document documento = documentBuilder.parse(archivo); //Fails here
        //Do some stuff
    } catch(XPathExpressionException | DOMException | SAXException | IOException | ParseException e1){
        throw new XmlParserException("Error when parsing XML: " + e1);
    }
}
解决方案

问题根源在于DocumentBuilder.parse(File)方法在解析失败时,可能未正确关闭文件句柄,导致文件被JVM进程占用,无法执行移动操作。以下是两种可靠修复方式:

方式一:使用InputStream读取并自动关闭资源

将readXml方法改为通过FileInputStream读取文件,利用try-with-resources语法自动关闭流,确保无论解析成功或失败,都能释放文件句柄:

private void readXml(String rutaFichero) throws XmlParserException{
    DocumentBuilder documentBuilder = initializeDocumentBuilder();
    try (FileInputStream fis = new FileInputStream(rutaFichero)) {
        Document documento = documentBuilder.parse(fis);
        //Do some stuff
    } catch(XPathExpressionException | DOMException | SAXException | IOException | ParseException e1){
        throw new XmlParserException("Error when parsing XML: " + e1);
    }
}

方式二:显式处理解析失败后的资源释放(兼容性略差)

若不想修改流读取方式,可在解析异常分支中尝试显式释放Document相关资源,但这种方式依赖具体DOM实现,兼容性不如方式一:

private void readXml(String rutaFichero) throws XmlParserException{
    DocumentBuilder documentBuilder = initializeDocumentBuilder();
    Document documento = null;
    try {
        File archivo = new File(rutaFichero);
        documento = documentBuilder.parse(archivo);
        //Do some stuff
    } catch(XPathExpressionException | DOMException | SAXException | IOException | ParseException e1){
        if (documento != null) {
            ((org.w3c.dom.Node) documento).setUserData(null, null, null);
        }
        throw new XmlParserException("Error when parsing XML: " + e1);
    }
}

额外注意事项

  • 检查initializeDocumentBuilder()方法的自定义配置,确认没有持有文件资源的逻辑
  • 执行移动操作前,确保目标路径的文件夹已存在,避免因路径缺失引发其他异常

内容的提问来源于stack exchange,提问作者Raider

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 22:45:15