Java读取CVS文件

最近在做支付宝对账的一个项目,其中有一部分需要将账单下载下来,对里面的账单进行读取。完成对账的功能。

读取CVS文件中所有的内容:

package alipay.fund.trans.toaccount;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;


public class read_cvs {

	public static void main(String[] args) throws IOException {
	    File csv = new File("D:\\20881122486776680156_20190308.csv");  // CSV文件路径
	    BufferedReader br = null;
	    try {
	        //br = new BufferedReader(new FileReader(csv));
	        br = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\\\20881122486776680156_20190308.csv"),"gbk"));
	    } catch (FileNotFoundException e) {
	        e.printStackTrace();
	    }
	    String line = "";
	    String everyLine = "";
	    try {
	        List 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读取CVS文件_第1张图片

对读取的文本进行一下出来:

package alipay.fund.trans.toaccount;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;

public class Testread {


    public static void main(String[] args) {  
        try {  
            BufferedReader reader =new BufferedReader(new InputStreamReader(new FileInputStream("D:\\\\20881122486776680156_20190308.csv"),"gbk"));
            reader.readLine();//第一行信息,为标题信息,不用,如果需要,注释掉 
            String line = null;  
            while((line=reader.readLine())!=null){  
                String item[] = line.split(",");//CSV格式文件为逗号分隔符文件,这里根据逗号切分 
                
                String last = item[item.length-1];//这就是你要的数据了 
                //int value = Integer.parseInt(last);//如果是数值,可以转化为数值 
                System.out.println(last);  
            }  
        } catch (Exception e) {  
            e.printStackTrace(); 
        }  
    }  
}

运行结果:
Java读取CVS文件_第2张图片

你可能感兴趣的:(小问题)