Joel
Joel

Reputation: 1630

Java formatter - setting file directory

i am trying to create a text file in a folder (called AMCData). The file is called "File" (for the sake of this example).

I have tried using this code:

public static void OpenFile(String filename)
{
    try
    {
        f = new Formatter("AMCData/" + filename + ".txt");          
    }
    catch(Exception e)
    {
        System.out.println("error present");
    }
}

But before i get the chance to even place any text in it, the catch keeps being triggered.. Could anyone inform me why this is occuring?

more information:

Upvotes: 1

Views: 350

Answers (1)

Kilian Foth
Kilian Foth

Reputation: 14346

You're right, a Formatter(String) constructor needs the file to be present or createable. The most likely reason why a file cannot be created is that it references a folder that itself doesn't exist, so you should use the File.mkdirs() method, like this:

new File("AMCData").mkdirs();

Upvotes: 3

Related Questions