我正在使用StAX将新的Record对象添加到名为“ archive.xml”的XML文件中。通用方法是:
创建XMLEventReader和XMLEventWriter;
3.1如果文件是根据列表的第一项创建的,则只需使用writer将记录添加到“ archive.xml”;
3.2。如果文件存在,请从“ archive.xml”中读取所有内容,然后并行将其写入“ temp.xml”,然后最后添加新Record。用“ 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;
}
我对代码进行了一些更改-现在可以正常使用:
FileOutputStream
创建(不再调用file.createFile()
)。 FileOutputStream
可能在file.createFile()
之后又创建了一个文件。FileOutputStream
和FileInputStream
现在是具体对象,我将其明确关闭。当我调用XMLEventWriter
时,FileOutputStream
可能没有关闭它正在使用的writer.close()
。正常吗这里是代码:
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;
}