csv文件其实就是格式化的txt文件
2015-07-09 08:10
337 查看
csv文件其实就是格式化的txt文件,所以操作和txt文件差不多,直接上代码了:
<dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.ostermiller</groupId> <artifactId>utils</artifactId> <version>1.07.00</version> </dependency>
import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.util.Date; import org.apache.commons.lang.time.DateFormatUtils; import com.Ostermiller.util.CSVPrint; import com.Ostermiller.util.CSVPrinter; /** * * @author alex.wang * csv文件写入 * */ public class CsvFilePrinter{ private CSVPrint csvPrint; /** * * @param fileName 文件路径 * @param append 是否支持追加 * @throws IOException */ public CsvFilePrinter(String fileName,boolean append) throws IOException { File file = new File(fileName); if(!file.exists()){ csvPrint = new CSVPrinter(new FileWriter(fileName,append)); init(); }else{ csvPrint = new CSVPrinter(new FileWriter(fileName,append)); if(!append){ init(); } } } public void init() throws IOException{ write(new String[]{"id","mac","val","date"}); } public void write(String[] values) throws IOException { csvPrint.writeln(values); } public static void main(String[] args) throws Exception { String csvFile = "demo".concat("-").concat(DateFormatUtils.format(new Date(), "yyyyMMdd")).concat(".csv"); CsvFilePrinter print = new CsvFilePrinter(csvFile,false); for(int i=0;i<10;i++){ print.write(new String[]{"50001"+i,"C914"+i,Integer.toString(-80+i),DateFormatUtils.format(new Date(), "yyyy-MM-dd")}); } } }
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import com.Ostermiller.util.ExcelCSVParser; import com.Ostermiller.util.LabeledCSVParser; /** * * @author alex.wang * csv文件解析器 * */ public class CsvFileParser{ private LabeledCSVParser csvParser;//csv解析器,对于第一行的表头信息,自动加载为索引关键字 private int currLineNum = -1;//文件所读到行数 private String[] currLine = null;//用来存放当前行的数据 /* * 构造函数, * Param: in InputStream 要解析的信息流 * throws IOException */ public CsvFileParser(InputStream in) throws IOException { csvParser = new LabeledCSVParser(new ExcelCSVParser(in)); currLineNum = csvParser.getLastLineNumber(); } public CsvFileParser(String fileName) throws IOException { InputStream in = new FileInputStream(fileName); csvParser = new LabeledCSVParser(new ExcelCSVParser(in)); currLineNum = csvParser.getLastLineNumber(); } /* * 检查是否还有数据 * * return ture 还有一行数据,false 没有数据 */ public boolean hasMore() throws IOException { currLine = csvParser.getLine(); currLineNum = csvParser.getLastLineNumber(); if (null == currLine) return false; return true; } /* * 返回当前行数据,关键字所指向的数据 * param:String filedName 该行的表头 * return:String 返回当前行数据,关键字所指向的数据 */ public String getByFieldName(String fieldName) { return csvParser.getValueByLabel(fieldName); } /* * 关闭解析器 * * */ public void close() throws IOException { csvParser.close(); } /* * 读取当前行数据 * * return String[] 读取当前行数据 */ public String[] readLine() throws IOException { currLine = csvParser.getLine(); currLineNum = csvParser.getLastLineNumber(); return currLine; } public int getCurrLineNum(){ return currLineNum; } public static void main(String[] args) throws Exception { //创建解析信息流 InputStream in=new FileInputStream(new File("demo.csv")); //实例解析器CsvFileParser CsvFileParser parser=new CsvFileParser(in); //读取数据 while(parser.hasMore()){ System.out.print(parser.getByFieldName("time")+" ");//time 系表头数据 System.out.print(parser.getByFieldName("total")+" "); } parser.close(); } }
相关文章推荐
- Bootstrap 3 How-To #3 布局
- 高效程序员的7个特征
- 从Ubuntu12.04升级到Ubuntu 14.04之后,系统将无法启动
- 常见的端口号
- linear search DEMO
- 学会使用微信公众平台
- 【剑指Offer学习】【面试题50:树中两个结点的最低公共祖先】
- 南阳oj 题目25 A Famous Music Composer
- 三期_day01_需求分析和业务流程
- java序列化
- OC-Block
- Semaphore Objects
- Linux信号基础
- .NET实现Office Excel自定义公式 广泛应用于报表与数据分析
- mysql主从复制
- OC-内存管理
- Merge Intervals
- 听课笔记(二)
- [Objective-C] selector 简介
- 听课练习(二)