繁体   English   中英

使用 StAX 写入现有 xml 文件

[英]Writing to existing xml file using StAX

我正在使用 StAX 将新记录 object 添加到名为“archive.xml”的 XML 文件中。 一般方法是:

  1. 检查文件是否存在,如果不存在 - 创建新文件;
  2. 创建 XMLEventReader 和 XMLEventWriter;

    3.1 如果文件是根据列表的第一项创建的,只需使用 writer 将记录添加到“archive.xml”;

    3.2. 如果文件存在,则从“archive.xml”中读取所有内容并将其并行写入“temp.xml”,最后添加新记录。 用“temp.xml”重写“archive.xml”。

但是3.2项的最后阶段会抛出错误java.nio.file.FileSystemException: The process cannot access the file because it is being used by another process (in sun.nio.fs.WindowsException) 可能是什么问题?

    public boolean create(Record record) {
        String fileName="archive.xml"
        boolean flag = false;
        File file = new File(fileName);
        if (!file.exists()) {
            try {
                file.createNewFile();
                flag = true;
            } catch (IOException ioe) {
                System.err.println("Can't create new file");
                return false;
            }
        }
        //Create StAX reader and writer
        XMLEventReader reader=null;
        XMLEventWriter writer=null;
        try {
            XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
            XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
            XMLEventFactory eventFactory = XMLEventFactory.newInstance();

            if (flag) {//if writing to new file
                writer = xmlOutputFactory.createXMLEventWriter(new FileOutputStream(file));
                /*using writer here*/
            writer.close();
            } else {//if adding to existing file
                File temp=new File("temp.xml");
                writer = xmlOutputFactory.createXMLEventWriter(new FileOutputStream(temp));
                reader = xmlInputFactory.createXMLEventReader(new FileInputStream(file));
                while (reader.hasNext()) {
               /*reading from "archive.xml", writing to "temp.xml"
               in the end - adding new Record to "temp.xml"*/
                }
                    reader.close();
                    writer.close();
//trying to rewrite "archive.xml" with "temp.xml" (but cannot get access to file)
                   try {
 Files.move(Path.of(temp.toURI()),Path.of(file.toURI()),StandardCopyOption.REPLACE_EXISTING);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }

        } catch (FileNotFoundException fileNotFoundException) {
            System.err.println("File not found");
        } catch (XMLStreamException xmlStreamException) {
           xmlStreamException.printStackTrace();
        }finally {
            try {
                if(reader!=null)
                reader.close();
                if(writer!=null)
                writer.close();
            } catch (XMLStreamException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

我对我的代码进行了一些更改 - 现在它工作正常:

  1. 新文件(如果不存在)现在由FileOutputStream创建(不再调用file.createFile() )。 可能FileOutputStreamfile.createFile()之后再次创建了一个文件。
  2. FileOutputStreamFileInputStream现在是具体对象,我明确地关闭它们。 当我调用writer.close()时, XMLEventWriter可能没有关闭它正在使用的FileOutputStream 正常吗?

这是代码:

public boolean create(Record record) {
    String fileName="archive.xml"

    File file = new File(fileName);
    boolean flag;
    flag=  !file.exists();

    //Create StAX reader and writer
    XMLEventReader reader=null;
    XMLEventWriter writer=null;
    //Create output and input streams
    FileOutputStream os;
    FileInputStream is;

    try {
        XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
        XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
        XMLEventFactory eventFactory = XMLEventFactory.newInstance();

        if (flag) {//if writing to new file
           os=new FileOutputStream(file);
            writer = xmlOutputFactory.createXMLEventWriter(os);
            /*using writer here*/
        os.close();
        writer.close();
        } else {//if adding to existing file
            File temp = new File("temp.xml");
            os=new FileOutputStream(temp);
            writer = xmlOutputFactory.createXMLEventWriter(os);
            is=new FileInputStream(file) ;
            reader = xmlInputFactory.createXMLEventReader(is);
            while (reader.hasNext()) {
           /*reading from "archive.xml", writing to "temp.xml"
           in the end - adding new Record to "temp.xml"*/
            }
                is.close();
                reader.close();
                os.close();
                writer.close();
               //trying to rewrite "archive.xml" with "temp.xml"
               try {      Files.move(Path.of(temp.toURI()),Path.of(file.toURI()),StandardCopyOption.REPLACE_EXISTING);
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

    } catch (FileNotFoundException fileNotFoundException) {
        System.err.println("File not found");
    } catch (XMLStreamException xmlStreamException) {
       xmlStreamException.printStackTrace();
    }finally {
        try {
            if(is!=null)
                is.close();
            if (reader != null)
                reader.close();
            if(os!=null)
                os.close();
            if (writer != null)
                writer.close();
        } catch (XMLStreamException e) {
            e.printStackTrace();
        }
    }
    return false;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM