本文目錄一覽:
如何用java解析CSV文件
思想:先獲取csv文件的路徑,通過BufferedReader類去讀該路徑中的文件,使用readLine方法進行逐行讀取。
注意:使用readLine方法後會自動轉到下一行。因此在判斷是否為空後得先將讀取到的內容賦值給一變數,在循環中使用該變數即可。
public static void main(String[] args)
{
File csv = new File(“C:\\Users\\chenxumin\\Desktop\\Result.csv”); // CSV文件路徑
BufferedReader br = null;
try
{
br = new BufferedReader(new FileReader(csv));
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
String line = “”;
String everyLine = “”;
try {
ListString allString = new ArrayList();
while ((line = br.readLine()) != null) //讀取到的內容給line變數
{
everyLine = line;
System.out.println(everyLine);
allString.add(everyLine);
}
System.out.println(“csv表格中所有行數:”+allString.size());
} catch (IOException e)
{
e.printStackTrace();
}
}
java讀取csv文件
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;
public class Test{
public static void main(String[] args) {
HashtableString, String[] dict = new HashtableString, String[]();
try {
BufferedReader reader = new BufferedReader(new FileReader(“test.csv”));
String line = null;
while((line=reader.readLine())!=null){
String item[] = line.split(“,”);
String item2[] = new String[19];
System.arraycopy(item,1,item2,0,19);
dict.put(item[0],item2);
}
Enumeration e2 = dict.keys();
while (e2.hasMoreElements()) {
String key = (String) e2.nextElement();
System.out.println(key);
String[] dd = (String[])dict.get(key);
for (int i=0;idd.length;i++) {
System.out.print(dd[i]+”\t”);
}
System.out.println();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
java 讀取csv發送 foxmail
CSV(CommaSeparatedValue),Foxmail和Outlook導出的地址本都可以是csv文件。CsvJdbc提供了Java訪問csv文件的的JDBC驅動,它其實是把一個csv文件當做一個資料庫表來操作,提供簡單的查詢。在發送foxmail的時候比較方便而已。
如何讀取csv一行中的單個數據java
package com.han.csv.util;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class CSVFileUtil {
// CSV文件編碼
public static final String ENCODE = “UTF-8”;
private FileInputStream fis = null;
private InputStreamReader isw = null;
private BufferedReader br = null;
public CSVFileUtil(String filename) throws Exception {
fis = new FileInputStream(filename);
isw = new InputStreamReader(fis, ENCODE);
br = new BufferedReader(isw);
}
// ==========以下是公開方法=============================
/**
* 從CSV文件流中讀取一個CSV行。
*
* @throws Exception
*/
public String readLine() throws Exception {
StringBuffer readLine = new StringBuffer();
boolean bReadNext = true;
while (bReadNext) {
//
if (readLine.length() 0) {
readLine.append(“\r\n”);
}
// 一行
String strReadLine = br.readLine();
// readLine is Null
if (strReadLine == null) {
return null;
}
readLine.append(strReadLine);
// 如果雙引號是奇數的時候繼續讀取。考慮有換行的是情況。
if (countChar(readLine.toString(), ‘”‘, 0) % 2 == 1) {
bReadNext = true;
} else {
bReadNext = false;
}
}
return readLine.toString();
}
/**
*把CSV文件的一行轉換成字元串數組。指定數組長度,不夠長度的部分設置為null。
*/
public static String[] fromCSVLine(String source, int size) {
ArrayList tmpArray = fromCSVLinetoArray(source);
if (size tmpArray.size()) {
size = tmpArray.size();
}
String[] rtnArray = new String[size];
tmpArray.toArray(rtnArray);
return rtnArray;
}
/**
* 把CSV文件的一行轉換成字元串數組。不指定數組長度。
*/
public static ArrayList fromCSVLinetoArray(String source) {
if (source == null || source.length() == 0) {
return new ArrayList();
}
int currentPosition = 0;
int maxPosition = source.length();
int nextComma = 0;
ArrayList rtnArray = new ArrayList();
while (currentPosition maxPosition) {
nextComma = nextComma(source, currentPosition);
rtnArray.add(nextToken(source, currentPosition, nextComma));
currentPosition = nextComma + 1;
if (currentPosition == maxPosition) {
rtnArray.add(“”);
}
}
return rtnArray;
}
/**
* 把字元串類型的數組轉換成一個CSV行。(輸出CSV文件的時候用)
*/
public static String toCSVLine(String[] strArray) {
if (strArray == null) {
return “”;
}
StringBuffer cvsLine = new StringBuffer();
for (int idx = 0; idx strArray.length; idx++) {
String item = addQuote(strArray[idx]);
cvsLine.append(item);
if (strArray.length – 1 != idx) {
cvsLine.append(‘,’);
}
}
return cvsLine.toString();
}
/**
* 字元串類型的List轉換成一個CSV行。(輸出CSV文件的時候用)
*/
public static String toCSVLine(ArrayList strArrList) {
if (strArrList == null) {
return “”;
}
String[] strArray = new String[strArrList.size()];
for (int idx = 0; idx strArrList.size(); idx++) {
strArray[idx] = (String) strArrList.get(idx);
}
return toCSVLine(strArray);
}
// ==========以下是內部使用的方法=============================
/**
*計算指定文字的個數。
*
* @param str 文字列
* @param c 文字
* @param start 開始位置
* @return 個數
*/
private int countChar(String str, char c, int start) {
int i = 0;
int index = str.indexOf(c, start);
return index == -1 ? i : countChar(str, c, index + 1) + 1;
}
/**
* 查詢下一個逗號的位置。
*
* @param source 文字列
* @param st 檢索開始位置
* @return 下一個逗號的位置。
*/
private static int nextComma(String source, int st) {
int maxPosition = source.length();
boolean inquote = false;
while (st maxPosition) {
char ch = source.charAt(st);
if (!inquote ch == ‘,’) {
break;
} else if (‘”‘ == ch) {
inquote = !inquote;
}
st++;
}
return st;
}
/**
* 取得下一個字元串
*/
private static String nextToken(String source, int st, int nextComma) {
StringBuffer strb = new StringBuffer();
int next = st;
while (next nextComma) {
char ch = source.charAt(next++);
if (ch == ‘”‘) {
if ((st + 1 next next nextComma) (source.charAt(next) == ‘”‘)) {
strb.append(ch);
next++;
}
} else {
strb.append(ch);
}
}
return strb.toString();
}
/**
* 在字元串的外側加雙引號。如果該字元串的內部有雙引號的話,把”轉換成””。
*
* @param item 字元串
* @return 處理過的字元串
*/
private static String addQuote(String item) {
if (item == null || item.length() == 0) {
return “\”\””;
}
StringBuffer sb = new StringBuffer();
sb.append(‘”‘);
for (int idx = 0; idx item.length(); idx++) {
char ch = item.charAt(idx);
if (‘”‘ == ch) {
sb.append(“\”\””);
} else {
sb.append(ch);
}
}
sb.append(‘”‘);
return sb.toString();
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/309585.html