-
Notifications
You must be signed in to change notification settings - Fork 18
Easy CSV file input and output in Java
aburkov edited this page Jun 27, 2015
·
1 revision
With xpresso you can work with CSV files Java with the simplicity of Python.
Read from file:
try (HappyFile f = x.open("filename.txt","r","utf-8")) {
for (list<String> row : x.csv(f)) {
//do stuff
}
}
Or, simply:
list<list<String>> data = x.list(x.csv("filename.txt","r","utf-8"));
Write to file:
try (HappyFile f = x.open("filename.txt","w","utf-8")) {
for (list<?> row : data){
x.csv(f).writerow(row);
}
}
Or, simply:
try (HappyFile f = x.open("filename.txt","w","utf-8")) {
f.write(x.csv(data).toString());
}
Write to a StringBuilder:
StringBuilder builder = new StringBuilder();
for (list<?> row : data) {
x.csv(builder).writerow(row);
}
String cs = c.toString();
Or, simply:
String cs = x.csv(data).toString();