开发者

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:

  • The folder does not exist, i was hoping it would automatically create it
  • If it doesn't automatically create folders, could you please link me to how to do so?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜