尝试使用资源打印机

时间:2015-02-19 21:59:16

标签: filenotfoundexception printwriter try-with-resources

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

//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)----------------------------------------------------

0 个答案:

没有答案