如何以最好的方式写入java文件

时间:2015-05-05 04:56:45

标签: java filewriter bufferedwriter

我写了一个java代码来编写一个已知模式的文件(为了以后使用regex进行读取)。有没有更好的方法来编写代码? (我正在努力改进我的编码)。

public void writeRecipe(Recipe r)
{
    // TODO Auto-generated method stub
    File file = new File(r.getPath());
    try {
        FileWriter filewriter = new FileWriter(file);
        BufferedWriter bufferwirte = new BufferedWriter(filewriter);
        filewriter.append(r.getType() + " Recipe:" + "\t"  + r.getName() +"\n");
        filewriter.append("\t" + "Ingredients List:\n");
        for (Map.Entry<String, String> entry : r.getIngredients().entrySet())
        {
            filewriter.append(entry.getKey() + " " + entry.getValue() + "\n");

        }
        filewriter.append("\n\tDirections:\n" );
        for(String dir :r.getDirections())
        {
            filewriter.append(dir + "\n");
        }
        bufferwirte.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.err.println("WritingFileRecipeErr: " + e );
    }
}

0 个答案:

没有答案