这个类编译后运行,可以看到在C盘根目录下生成了一个名字为helloCsv.csv的CSV文件,双击打开,如下的样子。
分析CSV文件
package com.vogoal.test; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; /** * @author SinNeR * * analysis a CSV file */ public class HelloCSVAnalysis { public static void main(String[] args) { InputStreamReader fr = null; BufferedReader br = null; try { fr = new InputStreamReader(new FileInputStream( "C:\\helloCsv.csv")); br = new BufferedReader(fr); String rec = null; String[] argsArr = null; while ((rec = br.readLine()) != null) { System.out.println(rec); argsArr = rec.split(","); for (int i = 0; i < argsArr.length; i++) { System.out.println("num " + (i + 1) + ":" + argsArr[i]); } } } catch (IOException e) { e.printStackTrace(); }finally{ try{ if ( fr != null ) fr.close(); if ( br != null ) br.close(); }catch(IOException ex){ ex.printStackTrace(); } } } }
|