我创建 xml 文件如下:

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
            DocumentBuilder dBuilder; 
            try { 
                dBuilder = dbFactory.newDocumentBuilder(); 
                Document doc = dBuilder.newDocument(); 
                //add elements to Document 
                Element rootElement = doc.createElement("document"); 
                //append root element to document 
                doc.appendChild(rootElement); 
 
            // Répertoire de destination 
            Element typeDoc = doc.createElement("typeDocument"); 
            typeDoc.appendChild(doc.createTextNode("En Attente")); 
            rootElement.appendChild(typeDoc); 
 
            // métadonnées : 
            Element properties = doc.createElement("properties"); 
            rootElement.appendChild(properties); 
 
            if (typo != null && !typo.isEmpty()) { 
                // 3) Type de document (name-type-value) 
                Element typoProperty = doc.createElement("property"); 
                properties.appendChild(typoProperty); 
                Element typoName = doc.createElement("name"); 
                typoName.appendChild(doc.createTextNode("os:typologie")); 
                typoProperty.appendChild(typoName); 
                Element typoType = doc.createElement("type"); 
                typoType.appendChild(doc.createTextNode("d:text")); 
                typoProperty.appendChild(typoType); 
                Element typoValue = doc.createElement("value"); 
                typoValue.appendChild(doc.createTextNode(typo)); 
                typoProperty.appendChild(typoValue); 
            } 
 
            ...... 
 
 
            //for output to file, console 
            TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
            Transformer transformer = transformerFactory.newTransformer(); 
            //for pretty print 
            transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
            DOMSource source = new DOMSource(doc); 
            logger.debug("nom xml: {}", xmlFolder + "/" + newFileName + ".xml"); 
            //write to console or file 
            // StreamResult console = new StreamResult(System.out); 
            StreamResult file = new StreamResult(new FileOutputStream(new File(xmlFolder + "/" + newFileName + ".xml"))); 
            //write data 
            // transformer.transform(source, console); 
            transformer.transform(source, file); 
 
        } catch (Exception e) { 
            logger.debug("Erreur création XML: {}", ExceptionUtils.getStackTrace(e)); 
            return false; 
        } 

此文件是在特定文件夹中创建的。该文件夹由 java 进程扫描,该进程首先移动 xml 文件:

  try { 
        String path = dayDate != null ? destinationParentFolderPath + "/" + destinationFolderName + "/" + dayDate + "-" + file.getName()  
                                      : destinationParentFolderPath + "/" + destinationFolderName + "/" + file.getName(); 
        logger.debug("moveFileToWorkFolder: " + path); 
        return Files.move(file.toPath(), FileSystems.getDefault().getPath(path), StandardCopyOption.REPLACE_EXISTING); 
    } catch (IOException e) { 
        logger.debug("Error moveFileToWorkFolder: " +  ExceptionUtils.getStackTrace(e)); 
        e.printStackTrace (); 
        return null; 
    } 

但我仍然收到以下消息:

Error moveFileToWorkFolder: java.nio.file.FileSystemException: C:\alfresco\SCAN\152712_inconnu 51_DOCUMENTS ADM.xml -> C:\alfresco\SCAN\DOC_WORK\152712_inconnu 51_DOCUMENTS ADM.xml: Le processus ne peut pas accéder au fichier car ce fichier est utilisé par un autre processus. 

=> 翻译:无法访问该文件,因为该文件正在被另一个进程使用

每 20 秒扫描一次文件夹,经过几次尝试,xml 被很好地复制 我不明白为什么文件被锁定以及如何解决该问题...

请您参考如下方法:

它通过关闭 FileOutputStream 来工作

   FileOutputStream outFile = null; 
        try { 
              ... 
               outFile = new FileOutputStream(new File(xmlFolder + "/" + newFileName + ".xml")); 
                StreamResult file = new StreamResult(outFile); 
                transformer.transform(source, file); 
 
        } catch (Exception e) { 
            logger.debug("Erreur création XML: {}", ExceptionUtils.getStackTrace(e)); 
            return false; 
        } finally { 
            try { 
                outFile.close(); 
            } catch (IOException e) { 
                logger.debug("Erreur close FileOutputStream: {}", ExceptionUtils.getStackTrace(e)); 
            } 
        } 


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!