删除现有文件并在循环内使用 TransformerFactory 创建/写入数据到同一文件中

发布于 2025-01-09 00:07:27 字数 2066 浏览 0 评论 0原文

我想使用 XSL 转换 input.XML 文件并在循环中覆盖output.XML 文件,以便获得最终的output.XML 文件。

我能够做到这一点,但是它不是覆盖output.XML 文件,而是附加带有循环的所有迭代数据的同一文件。

为了解决这个问题,我尝试仅在使用 XSL 转换 input.XML 文件之前删除循环中的现有 output.XML 文件,但出现错误 -

java.nio.file.FileSystemException: Output.xml: The process cannot access the file because it is being used by another process.

因此,我无法删除现有文件,也无法覆盖输出.xml 文件。

任何人都可以帮忙解决这个问题吗? 我相信解决这些问题中的任何一个都会有所帮助。

谢谢

public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
    // TODO Auto-generated method stub      
    
    try {
        
        Double currentValue = 1.0;
        
        String inputXMLPath = "C:/MySystem/Input.xml";
        
        String outputXMLPath = "C:/MySystem/Output.xml";
        
        StreamSource inputStream = new StreamSource(inputXMLPath);
        
        FileOutputStream opStream = new FileOutputStream(new File(outputXMLPath));
        
        while (currentValue != 7.0) {
            String xslPath = "C:/MySystem/input.xsl";
            Path path = FileSystems.getDefault().getPath(outputXMLPath);
            Files.delete(path);
            
            performTransformation(xslPath, inputStream, opStream, outputXMLPath);
            StreamSource secondStream = new StreamSource(outputXMLPath);
            inputStream = secondStream;
            currentValue++;
            opStream.flush();
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public static void performTransformation(String xslPath, StreamSource inputStream, FileOutputStream opStream,
        String outputXMLPath) throws Exception {        
    
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = null;
    transformer = tFactory.newTransformer(new StreamSource(xslPath));
    transformer.transform(inputStream, new StreamResult(opStream));
    opStream.flush();
}

I want to transform the input.XML file with XSL and overwrite the output.XML file in a loop so that I get the final output.XML file.

I am able to do this, however instead of overwriting the output.XML file, it's appending the same file with all iteration data of loop.

To resolve this issue, I tried to delete the existing output.XML file in loop only just before transforming the input.XML file with XSL, but getting error -

java.nio.file.FileSystemException: Output.xml: The process cannot access the file because it is being used by another process.

So, I'm NOT able to delete the existing file also not able to overwrite the output.xml file.

Can anyone help on this pls.
I believe resolving any one of these issue should help out.

Thanks

public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
    // TODO Auto-generated method stub      
    
    try {
        
        Double currentValue = 1.0;
        
        String inputXMLPath = "C:/MySystem/Input.xml";
        
        String outputXMLPath = "C:/MySystem/Output.xml";
        
        StreamSource inputStream = new StreamSource(inputXMLPath);
        
        FileOutputStream opStream = new FileOutputStream(new File(outputXMLPath));
        
        while (currentValue != 7.0) {
            String xslPath = "C:/MySystem/input.xsl";
            Path path = FileSystems.getDefault().getPath(outputXMLPath);
            Files.delete(path);
            
            performTransformation(xslPath, inputStream, opStream, outputXMLPath);
            StreamSource secondStream = new StreamSource(outputXMLPath);
            inputStream = secondStream;
            currentValue++;
            opStream.flush();
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public static void performTransformation(String xslPath, StreamSource inputStream, FileOutputStream opStream,
        String outputXMLPath) throws Exception {        
    
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = null;
    transformer = tFactory.newTransformer(new StreamSource(xslPath));
    transformer.transform(inputStream, new StreamResult(opStream));
    opStream.flush();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云朵有点甜 2025-01-16 00:07:27

很确定您无法编辑或删除此文件,因为输入流仍然打开,这就是为什么它仍在使用,如果您可以关闭输入流然后删除 XML 文件,然后创建一个新文件,它应该可以正常工作。

假设文件总是被称为相同的东西,这应该适合您的用例。

此外,deleteIfExists(Path) 方法也很有用,因为它仍然会删除文件,但如果文件不存在,则不会引发异常。

Pretty sure you can't edit or delete this file because the input stream is still open, that's why it is still in use if you can close the input stream then delete the XML file and then make a new one it should work no problem.

This should fit your use case assuming the file is always called the same thing.

Also the deleteIfExists(Path) method is useful as it will still delete the file but will not throw an exception if it does not exist.

梦里泪两行 2025-01-16 00:07:27

您无法删除该文件,因为您尚未关闭它。但与其删除它,为什么不在循环内部而不是外部重新创建该文件:

public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
    // TODO Auto-generated method stub      *** DELETE THIS LINE ***
    
    try {
        
        Double currentValue = 1.0;
        
        String inputXMLPath = "C:/MySystem/Input.xml";
        
        String outputXMLPath = "C:/MySystem/Output.xml";
        
        StreamSource inputStream = new StreamSource(inputXMLPath);
        
        // *** DANGER! COMPARING DOUBLES IS PRONE TO ERRORS! ***
        while (currentValue != 7.0) {
            // *** USE TRY-WITH-RESOURCES TO ENSURE THE STREAM GETS CLOSED ***
            try (FileOutputStream opStream = new FileOutputStream(outputXMLPath)) {
        
                String xslPath = "C:/MySystem/input.xsl";
            
                performTransformation(xslPath, inputStream, opStream, outputXMLPath);
                StreamSource secondStream = new StreamSource(outputXMLPath);
                inputStream = secondStream;
                currentValue++;
            }
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

You can't delete the file because you haven't closed it. But instead of deleting it, why don't you re-create the file inside the loop, instead of outside it:

public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
    // TODO Auto-generated method stub      *** DELETE THIS LINE ***
    
    try {
        
        Double currentValue = 1.0;
        
        String inputXMLPath = "C:/MySystem/Input.xml";
        
        String outputXMLPath = "C:/MySystem/Output.xml";
        
        StreamSource inputStream = new StreamSource(inputXMLPath);
        
        // *** DANGER! COMPARING DOUBLES IS PRONE TO ERRORS! ***
        while (currentValue != 7.0) {
            // *** USE TRY-WITH-RESOURCES TO ENSURE THE STREAM GETS CLOSED ***
            try (FileOutputStream opStream = new FileOutputStream(outputXMLPath)) {
        
                String xslPath = "C:/MySystem/input.xsl";
            
                performTransformation(xslPath, inputStream, opStream, outputXMLPath);
                StreamSource secondStream = new StreamSource(outputXMLPath);
                inputStream = secondStream;
                currentValue++;
            }
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文