java如何导出txt文件

hnnxzhoujia 2008-06-10 09:30:38
我想把我获得的数据(一个list),导出成固定路径的TXT文本文件
要导出的格式如下:
city ¦area_code ¦start_gt ¦hlr_name ¦ //这是表示数据库的字段
金华 ¦579 ¦861373892 ¦金华HLR3 ¦ //这是依次对应的数据
金华 ¦579 ¦861373893 ¦金华HLR3 ¦
金华 ¦579 ¦861373894 ¦金华HLR3 ¦
金华 ¦579 ¦861373895 ¦金华HLR3 ¦

我做过一个导出EXCEL文件的例子,但导出TXT没用过.哪位有这方面的开发经验,请教一下,生成TXT用哪个类啊,我是开发新手,请高人详细指点下,谢谢
我导出EXCEL的文件如下:
public class ExportExcel {

public static byte[] toSellExcel(String[] column, List list, String filename,
String[] excelFile) {

byte[] bytes = null;
try {
// 创建新的Excel 工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
// 在Excel工作簿中建一工作表,其名为缺省值
HSSFSheet sheet = workbook.createSheet();
// 设置Excel的显示格式
HSSFRow row;
HSSFCell cell;
HSSFCellStyle style;
HSSFFont hf;

for (int i = 0; i < 1; i++) {
// 在索引0的位置创建行(最顶端的行)
row = sheet.createRow((short) i);
for (int j = 0; j < column.length; j++) {
// 在索引0的位置创建单元格(左上端)(相当于列数)
cell = row.createCell((short) j);
// 定义单元格为字符串类型
cell.setCellType(HSSFCell.CELL_TYPE_STRING);

// 定义样式
style = workbook.createCellStyle();
// 创建工作本的字体
hf = workbook.createFont();
hf.setFontName("楷体_GB2312");
style.setFont(hf);
// 设置列的属性
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(style);
// 在所创建的单元格填入值
if (i == 0) {
cell.setCellValue(column[j]);
}
}
}

for (int i = 0; i < list.size(); i++) {

Object obj = list.get(i);
Class objclass = obj.getClass();
Field[] fields = obj.getClass().getDeclaredFields();
int count=0;
for (int j = 0; j < fields.length; j++) {
Field subField = fields[j];
String subName = subField.getName();
boolean flag = true;
for (int k = 0; k < excelFile.length; k++) {
if (excelFile[k].equals(subName)) {
flag = false;
break;
}
}
if (flag) {
String getFieldMethod = "get"
+ subName.substring(0, 1).toUpperCase()
+ subName.substring(1, subName.length());
Class FieldClass = subField.getType();
String FieldClassName = FieldClass.getName();
String value = "";
if (!FieldClass.isArray()) {
Method method = null;
try {
method = obj.getClass().getMethod(
getFieldMethod, null);
} catch (Exception e1) {
e1.printStackTrace();
}
if (method != null) {
Object methodValue = (Object) method.invoke(
obj, null);
if (methodValue != null) {
value = methodValue.toString();
}
}
}
row = sheet.createRow((short) i + 1); // 从第1行开始
// 在索引0的位置创建单元格(左上端)(相当于列数)
cell = row.createCell((short) count);
count++;
// 定义单元格为字符串类型
cell.setCellType(HSSFCell.CELL_TYPE_STRING);

// 定义样式
style = workbook.createCellStyle();
// 创建工作本的字体
hf = workbook.createFont();
hf.setFontName("楷体_GB2312");
style.setFont(hf);
// 设置列的属性
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(style);
// 在所创建的单元格填入值

cell.setCellValue(value);
}

}

}
// 新建一输出文件流


bytes= workbook.getBytes();


} catch (Exception e) {
System.out.println("已运行 xlCreate() :错误信息 " + e);
e.printStackTrace();
}
return bytes;
}


}
...全文
6003 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
foreverfeng168 2012-11-01
  • 打赏
  • 举报
回复
福德宫
liu517139239 2011-12-19
  • 打赏
  • 举报
回复
在grid中怎么实现呢
zyn8java 2010-12-20
  • 打赏
  • 举报
回复
dfd
cherish_yhy 2010-04-28
  • 打赏
  • 举报
回复
7楼的“\n”前面要加\r才能出来换行效果
fishinghaoyu 2009-08-11
  • 打赏
  • 举报
回复
紧急啊…………#
tiantian511 2009-02-01
  • 打赏
  • 举报
回复
...这样做的话,打开保存好的txt文件后会发现格式不是你设置的换行
而是一整排,怎么解决呢?
wxg1008 2008-06-10
  • 打赏
  • 举报
回复
jsp的,导出文件名为table.txt

<%
String txt = (String)request.getAttribute("txt");
response.setContentType("application/txt");
response.setHeader("Content-disposition", "attachment;filename=table.txt");
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(response.getOutputStream());
bos.write(txt.getBytes());
} catch (IOException e) {
throw e;
} finally {
if (bos != null)
bos.close();
}

%>
saintlythinking 2008-06-10
  • 打赏
  • 举报
回复
用 java 的自身的 io就行
不用引入外部组件,
书上就有例子,所以不重复了
reality 2008-06-10
  • 打赏
  • 举报
回复
其实真的很简单。。。
jyq0105 2008-06-10
  • 打赏
  • 举报
回复
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;


File dirFile = new File("c:\\aaaa");
if(!dirFile.exists()){
dirFile.mkdirs();
}
//查找文件,如果不存在,就创建
File file = new File("c:\\aaaa\\"+"AAA.txt");


if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

PrintWriter out=null;
try {
out = new PrintWriter(new BufferedWriter(new FileWriter("c:\\aaaa\\"+"AAA.txt")));
// 将数据写入文件
out.write("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
//关闭流
out.close();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
becloud 2008-06-10
  • 打赏
  • 举报
回复
dataSource就是你的list啊
里面应该是你的全部封装好的Bean吧
要是你的sysLogsService.querySysLogs(sql)没错的话!
hnnxzhoujia 2008-06-10
  • 打赏
  • 举报
回复
我的数据 List list = sysLogsService.querySysLogs(sql);//调用服务层的方法获得
把list进行导出
lixq2000 2008-06-10
  • 打赏
  • 举报
回复
我感觉文件读写就行了!
hnnxzhoujia 2008-06-10
  • 打赏
  • 举报
回复
dataSource是什么意思
becloud 2008-06-10
  • 打赏
  • 举报
回复

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class ExportUtil {
/**
* @param dataSource
* @param file
*/
public void export(List<MyBean> dataSource,String file){
FileWriter fw=null;
MyBean myBean=null;
String city=null;
String area_code=null;
String start_gt=null;
String hlr_name=null;
StringBuffer row=null;
try{
fw=new FileWriter(file);
row=new StringBuffer();
row.append("city\t\t").append("area_code\t\t").append("start_gt\t\t").append("hlr_name"+"\n");
fw.write(row.toString());
for(int i=0;i<dataSource.size();i++){
row=new StringBuffer();
myBean=dataSource.get(i);
city=myBean.getCity();
area_code=myBean.getArea_code();
start_gt=myBean.getStart_gt();
hlr_name=myBean.getHlr_name();
row.append(city+"\t\t").append(area_code+"\t\t").append(start_gt+"\t\t").append(hlr_name+"\n");
fw.write(row.toString());
}
fw.flush();
}catch(IOException e){

}finally{
if(fw!=null)
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]){
List dataSource=new ArrayList<MyBean>();
dataSource.add(new MyBean("金华","579","861373892","金华HLR3"));
dataSource.add(new MyBean("金华","579","861373893","金华HLR3"));
dataSource.add(new MyBean("金华","579","861373894","金华HLR3"));
dataSource.add(new MyBean("金华","579","861373895","金华HLR3"));
ExportUtil exportUtil=new ExportUtil();
exportUtil.export(dataSource, "F:/JavaScript/软件说明.txt");
}
}
class MyBean{
//假设你的数据库字段都是varchar类型的
private String city;
private String area_code;
private String start_gt;
private String hlr_name;

public MyBean() {
super();
}
public MyBean(String city, String area_code, String start_gt, String hlr_name) {
super();
this.city = city;
this.area_code = area_code;
this.start_gt = start_gt;
this.hlr_name = hlr_name;
}
public String getArea_code() {
return area_code;
}
public void setArea_code(String area_code) {
this.area_code = area_code;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getHlr_name() {
return hlr_name;
}
public void setHlr_name(String hlr_name) {
this.hlr_name = hlr_name;
}
public String getStart_gt() {
return start_gt;
}
public void setStart_gt(String start_gt) {
this.start_gt = start_gt;
}
}

不知道是不是你需要的
qqqqqwwqqq 2008-06-10
  • 打赏
  • 举报
回复
用流实现

首先要创建一个a.txt

然后用流往a.txt里写就可以了 实在想不会明天我给你代码
hnnxzhoujia 2008-06-10
  • 打赏
  • 举报
回复
我的问题 ,就是不知道怎么导出TXT文件啊.
不知道怎么下手.
burningice44 2008-06-10
  • 打赏
  • 举报
回复
用流应该可以实现
laorer 2008-06-10
  • 打赏
  • 举报
回复
为什么不直接组装成字符串?然后用java.io的一些类来操作呢?

FileOutputStream();
FileWriter();

你到网上随便查下java文件读写,应该蛮多的
happy002 2008-06-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 java2000_net 的回复:]
你的问题呢?
[/Quote]
在最上面,~
加载更多回复(2)

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧