附加带有标题的java文件

时间:2016-04-01 18:07:51

标签: java

感谢这个社区,我已经阅读了很多关于如何在java中将信息写入txt文件的方法的教程。但是如果我想要格式化这个文件并在文件的每个部分下写信息呢?

我在myfile.txt中尝试做的事情的例子是:

                    list of Apartments
                 ========================

Region===============Country==========Adresse =======PriceMunicipality 
writeRegionHere  writeCountryHere   writeAdressHere  writeMunicipalityHere 

每次添加新公寓时,必须在最后添加信息对我来说这是一个简单的步骤,我只是不知道如何使用这种格式编写它们

很多欣赏

2 个答案:

答案 0 :(得分:2)

您可以使用它来格式化文本

    private static void printRow(String c0, String c1, String c2, String c3 ) {
    System.out.printf("%-20s %-20s %-20s %-20s%n", c0, c1, c2, c3);
}
public static void main(String[] args) {

        printRow(" ", "", "List of Appartments", " ");
        printRow( "==============", "==============", "==============", "==============");

        printRow("Region", "Country", "Adresse", "PriceMunicipilaty");
        printRow("==============", "==============", "==============", "==============");
        printRow("Adress1", "Adress1" , "Adress1", "Adress2");
}

答案 1 :(得分:1)

如果您在.txt文件中输出,.txt文件本身不支持任何真正的格式,如粗体,下划线等,与.doc,.odt不同。所以要格式化文本,你必须按照输出正常的方式硬化它。您可以使用' \ t',' \ n'还有更多特殊字符可以根据需要显示输出,就像@Drogian所写的一样。

相关问题