「java读取csv」java读取csv存入list
本篇文章给大家谈谈java读取csv,以及java读取csv存入list对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java读取CSV文件
这是我写的个类 你参考下 其实那个引号是不用管的
public class CsvUtil1 {
private String filename = null;
private BufferedReader bufferedreader = null;
private List list = new ArrayList();
public CsvUtil1() {
}
public CsvUtil1(String filename) throws IOException {
this.filename = filename;
bufferedreader = new BufferedReader(new FileReader(filename));
String stemp;
while ((stemp = bufferedreader.readLine()) != null) {
list.add(stemp);
}
}
public List getList() throws IOException {
return list;
}
public int getRowNum() {
return list.size();
}
public int getColNum() {
if (!list.toString().equals("[]")) {
if (list.get(0).toString().contains(",")) {
return list.get(0).toString().split(",").length;
} else if (list.get(0).toString().trim().length() != 0) {
return 1;
} else {
return 0;
}
} else {
return 0;
}
}
public String getRow(int index) {
if (this.list.size() != 0)
return (String) list.get(index);
else
return null;
}
public String getCol(int index) {
if (this.getColNum() == 0) {
return null;
}
StringBuffer scol = new StringBuffer();
String temp = null;
int colnum = this.getColNum();
if (colnum 1) {
for (Iterator it = list.iterator(); it.hasNext();) {
temp = it.next().toString();
scol = scol.append(temp.split(",")[index] + ",");
}
} else {
for (Iterator it = list.iterator(); it.hasNext();) {
temp = it.next().toString();
scol = scol.append(temp + ",");
}
}
String str = new String(scol.toString());
str = str.substring(0, str.length() - 1);
return str;
}
public String getString(int row, int col) {
String temp = null;
int colnum = this.getColNum();
if (colnum 1) {
temp = list.get(row).toString().split(",")[col];
} else if (colnum == 1) {
temp = list.get(row).toString();
} else {
temp = null;
}
return temp;
}
public void CsvClose() throws IOException {
this.bufferedreader.close();
}
public void test() throws IOException {
CsvUtil1 cu = new CsvUtil1("D:/学习/00dw.csv");
List tt = cu.getList();
for (Iterator itt = tt.iterator(); itt.hasNext();) {
System.out.println(itt.next().toString()+"||");
}
// System.out.println(cu.getRowNum());
// System.out.println(cu.getColNum());
// System.out.println(cu.getRow(0));
// System.out.println(cu.getCol(0));
// System.out.println(cu.getString(0, 0));
cu.CsvClose();
}
public void createCsvTest1(HttpServletResponse Response) throws IOException {
CsvUtil1 cu = new CsvUtil1("D:/学习/00dw.csv");
List tt = cu.getList();
String data = "";
SimpleDateFormat dataFormat = new SimpleDateFormat("yyyyMMddHHmm");
Date today = new Date();
String dateToday = dataFormat.format(today);
File file=new File("D:/学习/001dw.csv");
if(!file.exists())
file.createNewFile();
// else
// file.delete() ;
String str[] ;
StringBuilder sb = new StringBuilder("");
BufferedWriter output=new BufferedWriter(new FileWriter(file,true));
for (Iterator itt = tt.iterator(); itt.hasNext();) {
String fileStr = itt.next().toString() ;
str = fileStr.split(",");
for(int i=0;i=str.length-1;i++){ //拆分成数组 用于插入数据库中
System.out.print("str["+i+"]="+str[i]+" ");
}
System.out.println("");
sb.append(fileStr+"\r\n") ;
}
//System.out.println(sb.toString());
output.write(sb.toString());
output.flush() ;
output.close();
cu.CsvClose();
}
public static void main(String[] args) throws IOException {
CsvUtil1 test = new CsvUtil1();
//test.test();
HttpServletResponse response = null ;
test.createCsvTest1(response);
}
}
如何用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发送 foxmail
CSV(CommaSeparatedValue),Foxmail和Outlook导出的地址本都可以是csv文件。CsvJdbc提供了Java访问csv文件的的JDBC驱动,它其实是把一个csv文件当做一个数据库表来操作,提供简单的查询。在发送foxmail的时候比较方便而已。
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的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java读取csv存入list、java读取csv的信息别忘了在本站进行查找喔。
发布于:2022-11-23,除非注明,否则均为
原创文章,转载请注明出处。