我正在使用 Java 将表格格式打印到文本文件中。我有三列和三行。诀窍是让列对齐。找到它们之间的间距的方法是什么?下面的代码不起作用,因为它设置了列之间的间距
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class makeTable {
static ArrayList<String> name = new ArrayList<String>();
static ArrayList<String> model = new ArrayList<String>();
static ArrayList<String> year = new ArrayList<String>();
public static void main(String arg[]) {
addData();
BufferedWriter writeTable = null;
try {
writeTable = new BufferedWriter(new FileWriter("/home/oveledar/Desktop/table.txt"));
for (int i = 0; i < name.size(); i++){
writeTable.write(name.get(i) + " " + model.get(i) + " " + year.get(i));
writeTable.newLine();
}
} catch (IOException e) {
System.err.println(e);
} finally {
if (writeTable != null) {
try {
writeTable.close();
} catch (IOException e) {
System.err.println(e);
}
}
}
}
public static void addData(){
name.add("Dodge");
name.add("BMW");
name.add("VW");
model.add("Caravan");
model.add("323i");
model.add("Passat");
year.add("2006");
year.add("2009");
year.add("2011");
}
输出
Dodge Caravan 2006
BMW 323i 2009
VW Passat 2011
"/t" 不适用于长名称
Dodge Caravan 2006
BMW 323i 2009
VW Long model name here 2011