如何在Java中以表格格式创建此文本文件?

时间:2013-11-28 13:33:21

标签: java swing

我需要以下列格式编写文本文件。

+--------+-------------------------------+-------------+
| ID     | Name                          | Date        |
+--------+-------------------------------+-------------+ 
| 0      | Ramesh Tendulkar              | 2008-12-31  |
|--------|-------------------------------|-------------|
| 1      | Venkatraman Raju Gopal Acharya| 2009-01-22  |
|--------|-------------------------------|-------------|
| 2      | John Machleyn                 | 2009-01-31  |        
+--------+-------------------------------+-------------+

请注意,在第二列Name中,填充的数据长度具有可变长度(取决于值)。

有人可以告诉我最简单的方法吗?我编写了以下代码来创建文本文件:

String string = "Report";
                System.out.println(string);
                File file = new File("D:/Distribution_list.txt");
                FileWriter fileWriter;
                try {
                    fileWriter = new FileWriter(file);

                    BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
                    bufferedWriter.newLine();
                    bufferedWriter.write(string);
                    bufferedWriter.newLine();

                    for(int i=0;i<string.length();i++){
                        bufferedWriter.write("-");
                    }
                    bufferedWriter.newLine();
                    bufferedWriter.newLine();

                    bufferedWriter.write("+");

                    for(int i=0;i<string.length()/2;i++){
                        bufferedWriter.write("-");
                    }

                    bufferedWriter.write("+");

                    for(int i=0;i<string.length()/2;i++){
                        bufferedWriter.write("-");
                    }

                    bufferedWriter.write("+");
                    bufferedWriter.newLine();

                    bufferedWriter.write("|");
                    String header_name_1 = "ID";
                    String header_name_2 = "Name";
                    String header_name_3 = "Date";

                    int len1 = string.length()/2 - header_name_1.length();
                    int len2 = string.length()/2 - header_name_2.length();
                    int len3 = string.length()/2 - header_name_3.length();

                    bufferedWriter.write(header_name_1);

                    bufferedWriter.write("\t");
                    bufferedWriter.write("\t");

                    bufferedWriter.write("+");

                    bufferedWriter.write(header_name_2);

                    bufferedWriter.write("\t");
                    bufferedWriter.write("\t");

                    bufferedWriter.write("+");

                    bufferedWriter.write(header_name_3);

                    bufferedWriter.write("\t");
                    bufferedWriter.write("\t");

                    bufferedWriter.write("+");

                    bufferedWriter.newLine();

                    bufferedWriter.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

//end of code..

但是上面的代码按以下格式创建文件:

  

报告

     

+ -------------------------------- ------------- + ------------------- + | ID +姓名+日期+

你现在可以帮助更容易吗?

1 个答案:

答案 0 :(得分:1)

    ArrayList<StackFile> arrayFile = new ArrayList<StackFile>(); 
    arrayFile.add(new StackFile("2", "Ramesh Tendulkar", "2008-12-31"));
    arrayFile.add(new StackFile("3", "John Machleyn", "2008-12-31"));


    FileReader fr = new FileReader("in.txt");
    BufferedReader br =new BufferedReader(fr); 
    String s;
    while((s = br.readLine()) != null) { 
        String[] spS = s.split("|")[1].split("+"); 
        arrayFile.add(new StackFile(spS[0],spS[1] ,spS[2]));
        } 
        fr.close(); 

    FileWriter fw = new FileWriter(new File("test.txt"));

    fw.write("+--------+-------------------------------+-------------+\n");
    fw.write("| ID     | Name                          | Date        |\n");
    fw.write("+--------+-------------------------------+-------------+\n");

    Iterator<StackFile> itS = arrayFile.iterator();

    while(itS.hasNext())
    {
        StackFile sf = itS.next();
        fw.write("| "+String.format("%-6s", sf.getID())+" | "+String.format("%-30s", sf.getName())+"| "+String.format("%-10s", sf.getDate())+"  |\n");
        fw.write((itS.hasNext())
                ?"|--------|-------------------------------|-------------|\n"
                :"+--------+-------------------------------+-------------+\n");

    }
    fw.close();

这个StackFile类:

public class StackFile {
private String ID;
private String Name;
private String Date;
public String getID() {
    return ID;
}
public void setID(String iD) {
    ID = iD;
}
public String getName() {
    return Name;
}
public void setName(String name) {
    Name = name;
}
public String getDate() {
    return Date;
}
public void setDate(String date) {
    Date = date;
}
public StackFile(String  _ID,String _name,String _date)
{
    ID=_ID;
    Name=_name;
    Date=_date;

} }