打印作者格式

时间:2014-03-06 08:43:59

标签: java format printwriter

我有一个包含多个对象的列表,每个对象包含三个strings,我应该如何格式化输出(在outfile.format()中)以使其在下面的示例输出中显示?

public void save() {
        try {

            PrintWriter outfile = new PrintWriter
                             (new BufferedWriter
                             (new FileWriter("reg_out.txt")));

        for (int i = 0; i < list.size(); i++)
        { 
            outfile.format("???", list.get(i).getLastName(),
                                        list.get(i).getFirstName(),
                                        list.get(i).getNumber());
        }

            outfile.close();

        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

    }

reg_out.txt中的示例输出:

Allegrettho     Albert          0111-27543
Brio            Britta          0113-45771
Cresendo        Crister         0111-27440

1 个答案:

答案 0 :(得分:2)

试试这个

outfile.printf("%-20s%-20s%-20s%n", list.get(i).getLastName(),
                                        list.get(i).getFirstName(),
                                        list.get(i).getNumber());

有关详细信息,请参阅java.util.Formatter API