Java io操作,poi导出excel,集合自带排序,日志报告

java io操作,poi导出到excel表格,sl4j日志打印,集合自带排序Comparator

要求:
取出txt文件中其中第50000行到60000行的数据,进行数据的解析,然后对数据的某一项进行排序,从小到大输出到excel表格中.每一步的错误进行日志打印,不要直接e.printStackTrace()抛到java堆栈中.打印时间,观察执行的时间差.


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;


import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ReadFile {


	private static final Logger logger = LoggerFactory
			.getLogger(ReadFile.class);
	private static final int STARTLINE = 50000;
	private static final int ENDLINE = 60000;


	public static void main(String[] args) {
		Long ts = System.currentTimeMillis();
		File file = new File("RCimp.txt");
		if (file.exists()) {
			System.err.println("exist");


			FileInputStream fis = null;
			InputStreamReader isr = null;
			BufferedReader br = null;


			try {
				fis = new FileInputStream(file);
				isr = new InputStreamReader(fis,"GBK");
				br = new BufferedReader(isr);


				String line;
				StringBuffer sb = new StringBuffer();
				int i = 0;
				List<Order> list = new ArrayList<Order>();
				while ((line = br.readLine()) != null) {
					i++;
					if (i >= STARTLINE && i <= ENDLINE) {
						//解析
						Order order = new Order();
						String[] s1 = line.split("\\(");
						order.setCode(s1[0]);
						String[] s = line.split("\\'");
						order.setName(s[1]);
						order.setNum(s[3]);
						order.setPoint(s[5]);
						order.setA(s[7]);
						order.setB(s[9]);
						order.setC(s[11]);
						list.add(order);
					}
				}
				Collections.sort(list, new NumComparator()); // 根据num排序
				exportExcel(list);

			} catch (FileNotFoundException e) {
				logger.info("{}", e.toString());
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				logger.info("{}", e.toString());
				e.printStackTrace();
			} catch (IOException e) {
				logger.info("{}", e.toString());
				e.printStackTrace();
			} finally {
				try {
					br.close();
					isr.close();
					fis.close();
				} catch (IOException e) {
					logger.info("{}", e.toString());
					e.printStackTrace();
				}
			}

		}
		Long te = System.currentTimeMillis();
		System.out.println(te - ts);
	}


	// 自定义比较器:按num排序
	static class NumComparator implements Comparator {
		public int compare(Object object1, Object object2) {// 实现接口中的方法
			Order o1 = (Order) object1; // 强制转换
			Order o2 = (Order) object2;
			return new Double(o1.getNum()).compareTo(new Double(o2.getNum()));
		}
	}

	public static void exportExcel(List<Order> list) {
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("银行账目表");
		// Create a row and put some cells in it. Rows are 0 based.
		int i = 0;//行数
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Order o = (Order) iterator.next();
			HSSFRow row = sheet.createRow(i);
			// Or do it on one line.
			row.createCell((short) 0).setCellValue(o.getCode());
			row.createCell((short) 1).setCellValue(o.getName());
			row.createCell((short) 2).setCellValue(o.getNum());
			row.createCell((short) 3).setCellValue(o.getPoint());
			row.createCell((short) 4).setCellValue(o.getA());
			row.createCell((short) 5).setCellValue(o.getB());
			row.createCell((short) 6).setCellValue(o.getC());
			i++;
		}
		// Write the output to a file
		try {
			FileOutputStream fileOut = new FileOutputStream(
					"new.xls");
			wb.write(fileOut);
			fileOut.close();
		} catch (FileNotFoundException e) {
			logger.info("{}", e.toString());
			e.printStackTrace();
		} catch (IOException e) {
			logger.info("{}", e.toString());
			e.printStackTrace();
		}


	}
}


思考:
对象:占用资源
这里是否需要对象?

你可能感兴趣的:(java,poi,IO,Excel,集合排序)