java读写CSV文件

Posted cloud-ken

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java读写CSV文件相关的知识,希望对你有一定的参考价值。

java读写CSV文件

觉得有用的话,欢迎一起讨论相互学习~Follow Me

参考文献
读取csv文件中数据
将数据保存为csv文件格式

读取CSV文件中的数据

import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;


public class readCSV 
    void readCSV(String readpath, ArrayList Nolist, ArrayList Dnalist) 
        File inFile = new File(readpath);
        try 
            BufferedReader reader = new BufferedReader(new FileReader(inFile));
            while (reader.ready()) 
                String line = reader.readLine();
                StringTokenizer st = new StringTokenizer(line, ",");
                int NO;
                String DNAsequence;

                if (st.hasMoreTokens()) 
                    NO = Integer.valueOf(st.nextToken().trim());
                    DNAsequence = String.valueOf(st.nextToken().trim());
                    Nolist.add(NO);
                    Dnalist.add(DNAsequence);
                
            
            reader.close();

         catch (Exception e) 

            e.printStackTrace();
        
    


将数据保存为csv格式

package Readcsv_testV0;


import java.io.*;
import java.util.ArrayList;

public class OutputCSV 
    public static void writeCSV(ArrayList<String []> DNA, ArrayList<Double> Con, ArrayList<Double> Hp, ArrayList<Double> Hm, ArrayList<Double> Si, ArrayList<Double> Tm, ArrayList<Double> GC, String path) 
        try 
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "UTF-8"));
            for (int i = 0; i < DNA.size(); i++) 
                out.write(DNA.get(i)[0]);
                out.write(",");
                out.write(DNA.get(i)[1]);
                out.write(",");
                out.write(Con.get(i).toString());
                out.write(",");
                out.write(Hp.get(i).toString());
                out.write(",");
                out.write(Hm.get(i).toString());
                out.write(",");
                out.write(Si.get(i).toString());
                out.write(",");
                out.write(Tm.get(i).toString());
                out.write(",");
                out.write(GC.get(i).toString());
                out.newLine();
            
            out.flush();
            out.close();
         catch (Exception e) 
            e.printStackTrace();
        

    

以上是关于java读写CSV文件的主要内容,如果未能解决你的问题,请参考以下文章

用Java对CSV文件进行读写操作

Java中csv文件读写分析

Python csv模块(读写文件)

java 读CSV 和 Excel

csv文件的读写

利用Javacsv实现Java读写csv文件