第一版:简单写对象
代码
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Demo2 {
public static void main(String[] args) throws UnsupportedEncodingException {
String fileName=new String("stuDate2.csv".getBytes(),"ISO-8859-1");
List<Stu> student=new ArrayList<>();
student.add(new Stu("0001","张三",23));
student.add(new Stu("0002","李四",24));
student.add(new Stu("0003","王五",22));
try {
File file = new File(fileName);
BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"GB2312"),1024);
writer.write("Id,Name,Age");
for (Stu stu:
student) {
writer.newLine();
StringBuffer sb2=new StringBuffer();
sb2.append(stu.getId()+"\t"+",");
sb2.append(stu.getName()+"\t"+",");
sb2.append(stu.getAge()+"\t"+",");
sb2.deleteCharAt(sb2.lastIndexOf(","));
writer.write(sb2.toString());
}
writer.flush();
writer.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public class Stu {
private String Id;
private String Name;
private int Age;
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 int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
public Stu(String id, String name, int age) {
Id = id;
Name = name;
Age = age;
}
}
执行结果

第二版:数据库中JDBC流的方式边读编写
…待续