我用java写了一个程序,读取一个.txt文本里的东西,也写了一个输出execl的程序,怎么把这两个程序组合

我想把读取.txt后这些数据,输出到execl中,这是两个程序,如何让他们组合到一起,然后实现读取后就输出呢,要是能帮我加一个读取这个.txt文本后,遍历里面的程序做匹配对应,是最好的,麻烦大神们了
附上的代码如下:读取的代码
public class demo{
/**
* 管理后台获取后台数据
* begin
* @throws WriteException
*/
public static void main(String[] args) {
String json_jiade = readFile("D:\\111.txt");
System.out.println("去掉注释前:");
System.out.println(json_jiade);
String str = json_jiade.replaceAll("[#].*", "");
System.out.println("去掉注释后:");
System.out.println(str);
}
/**读取本地文件
* @param remoteFile
* @return
*/
public static String readFile(String remoteFile) {
String log = "";
try {
File file = new File(remoteFile);
FileInputStream is = new FileInputStream(remoteFile);
int size = (int) file.length();
byte[] bytes = getBytes(is,size);
String content = new String(bytes, "UTF-8");
log = content;
is.close();
} catch (IOException ex) {
}
return log;
}
private static byte[] getBytes(InputStream inputStream,int size) {
byte[] bytes = new byte[size];
try {
int readBytes = inputStream.read(bytes);
return bytes;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
哪位大神可以帮我组合好,发给我,不胜感谢

将读到的,通过POI 写EXCEL。。。。。。。。如果简单的表格,可以写成,分隔的csv文本文件的
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-08-06
package com.buybal.setliq.scheduler.job;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.buybal.util.StringUtils;

public class Test {

@org.junit.Test
public void testRW(){
List<String> list = null;
try {
list = readTxt("E:/","test.txt");
} catch (IOException e) {
e.printStackTrace();
}
boolean flag = createCheckFile(list,"E:/test.xls");
System.out.println("操作"+flag);
}
public static List<String> readTxt(String filePath ,String fileName) throws IOException{
List<String> list = new ArrayList<String>();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath + fileName),"UTF-8"));
String lineStr = null;
while((lineStr = br.readLine())!=null && StringUtils.isNotEmpty(lineStr)){
list.add(lineStr);
}
return list;
}
public static boolean createCheckFile(List<String> list, String excelPath) {
String[] TITLE = {"列1","列2","列3","列4"};
String str = null;
Workbook workBook = new HSSFWorkbook();
Row row = null;
Cell cell = null;
Sheet sheet = workBook.createSheet("Sheet1");

row = sheet.createRow(0);
for (int i = 0; i < TITLE.length; i++) {
cell = row.createCell(i);
cell.setCellValue(TITLE[i]);
}
for (int i = 0; i < list.size(); i++) {
row = sheet.createRow(i + 1);
str = list.get(i);
cell = row.createCell(0);
cell.setCellValue(str);
cell = row.createCell(1);
cell.setCellValue(str);
cell = row.createCell(2);
cell.setCellValue(str);
cell = row.createCell(3);
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(excelPath);
workBook.write(fos);
fos.flush();
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (null != fos) {
try {
fos.close();
System.out.println("文件生成成功!");
return true;
} catch (IOException e) {
System.out.println("文件生成失败!");
e.printStackTrace();
return false;
}
}
}
return true;
}
}本回答被提问者采纳
相似回答