Last active
August 29, 2015 13:57
-
-
Save yuikns/9735612 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.argcv.util; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
public class CSVParser { | |
final static int defaultMaxWidth = 20; | |
public static String[] parse(BufferedReader br, int maxWidth) { | |
String[] dataItem = new String[maxWidth]; | |
int dataId = 0; | |
String line = ""; | |
try { | |
line = br.readLine(); | |
if (line == null) { | |
System.err.println("line is null"); | |
return null; | |
} | |
} catch (IOException e1) { | |
e1.printStackTrace(); | |
return null; | |
} | |
char[] charArray = line.toCharArray(); | |
boolean quotFlag = false; | |
boolean finishFlag = false; | |
StringBuffer sb = new StringBuffer(); | |
int arrayLen = charArray.length; | |
boolean again = false; | |
do { | |
if (again) { | |
try { | |
line = br.readLine(); | |
if (line == null) { | |
System.err.println("line is null"); | |
return null; | |
} else { | |
// System.err.println(line); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return null; | |
} | |
charArray = line.toCharArray(); | |
arrayLen = charArray.length; | |
} | |
for (int i = 0; i < arrayLen; i++) { | |
switch (charArray[i]) { | |
case ',': | |
if (!quotFlag) { | |
dataItem[dataId] = sb.toString(); | |
sb = new StringBuffer(); | |
dataId++; | |
if (dataId >= maxWidth) { | |
finishFlag = true; | |
break; | |
} | |
} else { | |
sb.append(","); | |
} | |
break; | |
case '\"': | |
if ((i == arrayLen - 1) || (charArray[i + 1] != '\"') || !quotFlag) { | |
quotFlag = quotFlag ? false : true; | |
} else { | |
sb.append("\""); | |
i++; | |
} | |
break; | |
default: | |
sb.append(charArray[i]); | |
} | |
// System.out.println(String.format("%c:%x:%s", charArray[i],(int)charArray[i],quotFlag?"true":"false")); | |
// System.out.println(charArray[i] + ":" + quotFlag); | |
if (finishFlag) | |
break; | |
} | |
again = true; | |
if(quotFlag) sb.append("\n"); | |
} while (quotFlag); | |
if (dataId < maxWidth) { | |
dataItem[dataId] = sb.toString(); | |
} | |
// System.err.println("jump out"); | |
return dataItem; | |
} | |
public static String[] parse(BufferedReader br) { | |
return parse(br, defaultMaxWidth); | |
} | |
public static void main(String[] args) throws FileNotFoundException { | |
BufferedReader br = new BufferedReader(new FileReader(new File("/home/yu/Downloads/nottebest.csv"))); // 文件名 | |
String[] fields = CSVParser.parse(br); // 调用,有两种方法,一种直接传入br,默认20列。否则第二个参数,比如CSVParser.parse(br,20); | |
while ((fields = CSVParser.parse(br)) != null) { // 返回null 为读文件结束 | |
for (String f : fields) { | |
System.out.print(f + "\t"); | |
} | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment