package yong;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.db.Jdbc_localhost;
/**
*
* @author 赵永恩
*
*/
public class ExportUtil {
/**
* 导出TXT文件
* @param dataSource数据列表
* @param file输出地方
*/
public void export(List dataSource, String file) {
FileWriter fw = null;
StringBuffer row = null;
try {
fw = new FileWriter(file);
row = new StringBuffer();
row.append("ID编号\t\t").append("姓名\t\t").append("内容\t\t").append("回复时间\r\n");
fw.write(row.toString());
for (int i = 0; i < dataSource.size(); ++i) {
row = new StringBuffer();
row.append(dataSource.get(0) + "\t\t").append(dataSource.get(1) + "\t\t").append(dataSource.get(2) + "\t\t").append(dataSource.get(3) + "\r\n");
fw.write(row.toString());
}
fw.flush();
} catch (IOException e) {
} finally {
if (fw != null)
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 测试
* @param args
*/
public static void main(String args[]) {
List li = new ArrayList();
Jdbc_localhost db=new Jdbc_localhost();
String sql="select * from hx_new_ly";
ResultSet rs=db.executeQuery(sql);
try {
while(rs.next()){
li.add(rs.getString("id"));
li.add(rs.getString("xingming"));
li.add(rs.getString("neirong"));
li.add(rs.getString("hftime"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ExportUtil exportUtil = new ExportUtil();
exportUtil.export(li, "F:/软件说明.txt");
System.out.println("ok");
}
}