1

我正在尝试学习如何使用资源。首先,我尝试将 java.io.File myFile = new java.io.File(filename) 放在资源括号中,但 netbeans 告诉我它不可自动关闭。我是否正确处理了这个异常?我的印象是异常将在我定义文件类对象的行中生成。

//This method writes to a csv or txt file, specify full filepath (including
//extension)  Each value will be on a new line
public void writeFile(String filename)
{
    java.io.File myFile = new java.io.File(filename);
    try(java.io.PrintWriter outfile = new java.io.PrintWriter(myFile))
    {
        for (int i = 0; i < size; i++)
        {
            //print all used elements line by line
            outfile.println(Integer.toString(this.getElement(i)));
        }
    } catch (FileNotFoundException fileNotFoundException)
    {
        //print error
    }

}//end writeFile(String)----------------------------------------------------
4

0 回答 0