Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6129871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:47:34+00:00 2026-05-23T16:47:34+00:00

I developed a desktop application, which has a class that reads a XML file

  • 0

I developed a desktop application, which has a class that reads a XML file (using DOM), then performs some operations to the data, and saves the data back to XML (replacing previous file). This class is instantiated and the method invoked every 30 seconds. The problem I have is that if the computer running the application is shutdown (suddenly with the power button, not properly). then, when pc starts again, the XML file is empty. A blank file remains. This doesn’t happen every single time the computer is shut down, but very often though. If the computer is properly turned off, this never happens.
This is the code:

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();
}

Any idea of why this happens? Is there anything wrong in the code? Is there any way to prevent this?
Thanks, Damian

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:47:35+00:00Added an answer on May 23, 2026 at 4:47 pm

    To prevent such thing, you could open your file and read it, perform some operations, save it in another file, and them replace the first file with the second (like a rename).

    This way, when you turn the computer off, you’ll have the old file.

    But this is not super-secure: there might happen to turn the computer off in the moment it’s replacing the file, leaving you with an corrupted file instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We've developed a desktop application using .NetFX3.5 which has some winforms and two WPF
I am writing a desktop application written in Swing developed using Java 1.5. Some
I developed a desktop application, it's almost done but still contains some bugs which
I have a legacy desktop accounting application developed using Delphi 5 & Paradox, which
I am developing a c# desktop application and using a webservies which is developed
I developp a Java application using Windows Desktop Search from which I can retrieve
As part of my Final Year Project I've developed a desktop application, which fits
The BlackBerry Desktop API has the interface IRimTable which apparently maps an application database
We are using ASP.Net Web services. The architecture is Desktop Based Application developed in
I have an application developed in C# for desktop which will access MySql Database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.