0

I'm trying to create a settings file for my program, but when I debug the program, I get an IOException.

Here's the code I'm using.

// Universal Android Toolkit
String docs = new String(System.getProperty("user.home"));
String uatDocs = docs + "/Team_M4gkBeatz/Universal_Android_Toolkit";
// Settings
File sets = new File(uatDocs + "/Settings/Settings_uat.uatsavefile");

private void createSettings()
{
    try
    {
        jProgressBar1.setValue(0);
        progress += "Creating settings file... ";
        jTextArea2.setText(progress);
        sets.createNewFile();
        Thread.sleep(3000);
        progress += "Done.\nLoading UI...\n";
        jTextArea2.setText(progress);
        jProgressBar1.setValue(100);
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, "ERROR: There was an error while creating the settings file.\n" + ex + "\nPlease report this error to the developer/s.\nThe application will now close.", "Error Creating Settings File", JOptionPane.ERROR_MESSAGE);
        System.exit(0);
    }
}

What am I doing wrong?

8
  • Are you sure the file is on the path you think it is? Print it out and verify. Commented Nov 2, 2013 at 19:57
  • 2
    What does the stacktrace say? Commented Nov 2, 2013 at 19:59
  • 2
    Post the exception you are getting (including complete stack trace). Commented Nov 2, 2013 at 20:00
  • I'm 100% positive it is. Look here: db.tt/bgeJoIxa Commented Nov 2, 2013 at 20:02
  • The error is: java.io.IOException: The system could not find the path specified. Commented Nov 2, 2013 at 20:03

1 Answer 1

1

Looks like you haven't upper folder

System.getProperty("user.home") + "/Team_M4gkBeatz/Universal_Android_Toolkit/Settings"

so you cann't create the file. So you should check it and create folders:

final File parentDirectory = sets.getParentFile();
if (!parentDirectory.exists()) // checks that directory exists
{
   parentDirectory.mkdirs();   
}
if (!sets.exists()) // checks that file exists
{
   sets.createNewFile();
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.