日期:2014-05-17 浏览次数:20696 次
package com.test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
*/
public class Test {
public static void main(String[] args) {
List<Stu> list = new ArrayList<Stu>();
Stu stu1 = new Stu("1","test1");
list.add(stu1);
Stu stu2 = new Stu("2","test2");
list.add(stu2);
File file = new File("d:\\test.dat");//写入的文件
try {
FileWriter fw = new FileWriter(file);
StringBuffer stb = new StringBuffer();
stb.append("id;name;");
fw.write(stb.toString()+"\n");//写入头
for (Stu stu : list) {//遍历数据行
stb = new StringBuffer();
stb.append(stu.getId()+";"+stu.getName()+";\n");
fw.write(stb.toString());//写入数据
}
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Stu{
public Stu(String id, String name){
this.id = id;
this.name = name;
}
String id;
String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}