Я разработал настольное приложение, которое имеет класс, который читает файл XML (используя DOM), затем выполняет некоторые операции с данными и сохраняет данные обратно в XML (заменяя предыдущий файл).Этот класс создается, и метод вызывается каждые 30 секунд.Проблема у меня заключается в том, что если компьютер, на котором запущено приложение, выключается (внезапно с кнопкой питания, не правильно).затем, когда компьютер запускается снова, файл XML пуст.Пустой файл остается.Это происходит не каждый раз, когда компьютер выключается, но очень часто.Если компьютер правильно выключен, это никогда не происходит.Это код:
private org.w3c.dom.Document dom;
private javax.xml.parsers.DocumentBuilder db;
public PlayerConfigHandler() {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
db = dbf.newDocumentBuilder();
dom = db.newDocument();
} catch (Exception e) { }
}
public void updateConfig(List<File> playlists) {
String playerPath = Preferences.getInstance().readStringProperty("PlayerDirectory");
File configuration = new File(playerPath + "\\playerconfiguration.sdpp"); // replace with configuration path
if(!configuration.exists()) {
Logger.getLogger(PlayerConfigHandler.class).info("File " + configuration
+ " does not exist! Unable to update it.");
return;
}
dom = db.newDocument();
InputStream is = null;
Reader reader = null;
try {
is = new FileInputStream(configuration);
reader = new InputStreamReader(is);
InputSource source = new InputSource(reader);
dom = db.parse(source);
} catch (SAXException ex) {
Logger.getLogger(PlayerConfigHandler.class).error("updateConfig(). SAXException reading XML file: " + ex);
return;
} catch (IOException ex) {
Logger.getLogger(PlayerConfigHandler.class).error("updateConfig(). IOException reading XML file: " + ex);
return;
} catch(Exception ex) {
Logger.getLogger(PlayerConfigHandler.class).error("updateConfig(). Exception reading XML file: " + ex);
return;
} finally {
try {
reader.close();
is.close();
} catch (Exception e) {
Logger.getLogger(PlayerConfigHandler.class).error("Exception closing FileInputStream/Reader after reading XML file: " + e);
}
}
Element element = dom.getDocumentElement();
//perform some operations to the data read...
FileOutputStream outputStream = null;
try {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute("indent-number", 4);
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty("encoding","ISO-8859-1");
Source source = new DOMSource(dom);
outputStream = new FileOutputStream(configuration);
Result result = new StreamResult(new OutputStreamWriter(outputStream));
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
} catch (TransformerConfigurationException tce) {
Logger.getLogger(PlayerConfigHandler.class).error("Exception saving XML file: " + tce);
} catch (TransformerException te) {
Logger.getLogger(PlayerConfigHandler.class).error("Exception saving XML file: " + te);
} catch (FileNotFoundException fe) {
Logger.getLogger(PlayerConfigHandler.class).error("Exception saving XML file: " + fe);
} catch(Exception e) {
Logger.getLogger(PlayerConfigHandler.class).error("Exception saving XML file: " + e);
}
finally {
try {
outputStream.close();
} catch (Exception e) {
Logger.getLogger(PlayerConfigHandler.class).error("Exception closing FileOutputStream after writing XML file: " + e);
}
}
dom = db.newDocument();
}
Есть идеи, почему это происходит?Что-то не так в коде?Есть ли способ предотвратить это?Спасибо, Дамиан