我创建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.
=>Translate:无法访问该文件,因为另一个进程正在使用该文件
该文件夹每20秒扫描一次,经过几次尝试后,xml被很好地复制
我不明白为什么文件被锁定以及如何解决这个问题。。。