I have to print list of objects to a text file with table format. For example, if I have list of Person(has getName,getAge and getAddress methods)objects, the text file shou
import java.util.*;
public class Test {
public static void main(String[] args) {
List<Person> list = new ArrayList<Person>();
list.add(new Person("alpha", "astreet", 12));
list.add(new Person("bravo", "bstreet", 23));
list.add(new Person("charlie", "cstreet", 34));
list.add(new Person("delta", "dstreet", 45));
System.out.println(String.format("%-10s%-10s%-10s", "Name", "Age", "Adress"));
for (Person p : list)
System.out.println(String.format("%-10s%-10s%-10d", p.name, p.addr, p.age));
}
}
class Person {
String name;
String addr;
int age;
public Person(String name, String addr, int age) {
this.name = name;
this.addr = addr;
this.age = age;
}
}
Output:
Name Age Adress
alpha astreet 12
bravo bstreet 23
charlie cstreet 34
delta dstreet 45
Use printf with padded fields to achive column alignments.
PrintWriter.printf to be specific