Java-创建和移动xml:由于另一个进程正在使用该文件而无法访问该文件

时间:2018-07-09 14:20:04

标签: java xml file-io java-8

我创建如下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;
    }

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

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.

但是我仍然收到以下消息:

def plot_confusion_matrix( cm_raw, title="Confusion Matrix", cmap=colors.ListedColormap( [
                       "lightgreen", "mistyrose", "lightyellow", "lightgray"] ) ):
    cm = cm_raw.astype( "float" ) / cm_raw.sum()

    plt.imshow( cm, interpolation="nearest", cmap=cmap )
    class_names = ["Negative", "Positive"]
    plt.title( title )
    plt.xlabel( "Predicted Label" )
    plt.ylabel( "True Label" )
    tick_marks  = np.arange( len( class_names ) )

    s = [["True Negative", "False Positive"], ["False Negative", "True Positive"]]
    for row_index in range( 2 ):
        for col_index in range( 2 ):
            plt.text( col_index, row_index, str( s[row_index][col_index]) + ":\n" + str( format(
            cm[row_index][col_index] * 100, ".2f" ) ) + "%", ha="center", color="black" )
    x_listsum = cm.sum( axis=0 ) * 100
    x_sum     = ["{0:.2f}%".format( x_index ) for x_index in x_listsum]
    y_listsum = cm.sum( axis=1 ) * 100
    y_sum     = ["{0:.2f}%".format( y_index ) for y_index in y_listsum]
    plt.xticks( tick_marks, x_sum )
    plt.yticks( tick_marks, y_sum )

    plt.show()

=>翻译:由于其他进程正在使用该文件,因此无法访问该文件

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

1 个答案:

答案 0 :(得分:0)

通过关闭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));
            }
        }