「csv文件java」csv文件java解析

博主:adminadmin 2023-03-20 14:24:05 282

本篇文章给大家谈谈csv文件java,以及csv文件java解析对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

如何用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文件

java"importjava.io.BufferedReader;importjava.io.FileReader;publicclassTest{publicvoidtest(introw,intcol){try{BufferedReaderreader=newBufferedReader(newFileReader("C:\\a.csv"));//换成你的文件名//reader.readLine();//第一行信息,为标题信息,不用,如果需要,注释掉Stringline=null;intindex=0;while((line=reader.readLine())!=null){Stringitem[]=line.split("");//CSV格式文件为逗号分隔符文件,这里根据逗号切分if(index==row-1){if(item.length=col-1){Stringlast=item[col-1];//这就是你要的数据了System.out.println(last);}}//intvalue=Integer.parseInt(last);//如果是数值,可以转化为数值index++;}}catch(Exceptione){e.printStackTrace();}}/***@paramargs*/publicstaticvoidmain(String[]args){Testtest=newTest();test.test(3,2);}}你的数据格式有问题,空格的个数不确定,没法每行用空格分隔。以下是我调整后的数据格式每行的数据以一个空格分隔,test方法传入的参数一次是,行,列:1电机12WBS23PID34CP5社供出6原価実绩7社供WC8外注费9直材费10自家制品11直経费12その他13注残14注残

如何用Java解析CSV文件

package com.test;

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class TestImportCsv {

private InputStreamReader fr = null;

private BufferedReader br = null;

public TestImportCsv(String f) throws IOException {

fr = new InputStreamReader(new FileInputStream(f));

}

/**

* 解析csv文件 到一个list中 每个单元个为一个String类型记录,每一行为一个list。 再将所有的行放到一个总list中

*/

public ListListString readCSVFile() throws IOException {

br = new BufferedReader(fr);

String rec = null;// 一行

String str;// 一个单元格

ListListString listFile = new ArrayListListString();

try {

// 读取一行

while ((rec = br.readLine()) != null) {

Pattern pCells = Pattern

.compile("(\"[^\"]*(\"{2})*[^\"]*\")*[^,]*,");

Matcher mCells = pCells.matcher(rec);

ListString cells = new ArrayListString();// 每行记录一个list

// 读取每个单元格

while (mCells.find()) {

str = mCells.group();

str = str.replaceAll(

"(?sm)\"?([^\"]*(\"{2})*[^\"]*)\"?.*,", "$1");

str = str.replaceAll("(?sm)(\"(\"))", "$2");

cells.add(str);

}

listFile.add(cells);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

if (fr != null) {

fr.close();

}

if (br != null) {

br.close();

}

}

return listFile;

}

public static void main(String[] args) throws Throwable {

TestImportCsv test = new TestImportCsv("D:/test.csv");

ListListString csvList = test.readCSVFile();

}

}

csv文件java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于csv文件java解析、csv文件java的信息别忘了在本站进行查找喔。