簡體   English   中英

將CSV文件導出到數組中

[英]Exporting a CSV file into arrays

我有一個包含不同列數據的CSV文件。

例:

100, 0.1, 0.5
200, 0.1, 0.1
300, 0.2, 0.5

等等

我想將第一列存儲到數組中,將第二列存儲到自己的數組中,依此類推,以便可以將它們用於數學方程式。

import.java.util.Scanner; 
import java.io.File;

public class Example
{
    public static void main(String[] args) throws Exception 
    {
        Scanner s = new Scanner(new File("C://java//data.txt"));
    }
}

到目前為止,我還很迷茫。

You can try this:

public class ReadCSV {
    public static List<List<Double>> getDimensionList(String key) throws Exception  
    {        
        List<List<Double>> listOfDimension = new ArrayList<List<Double>>();
        List<Double> dimensionList = null;      
        File file = null;
        BufferedReader br = null;
        try {
            file = new File(key);   
            br = new BufferedReader(new FileReader(file));

            String strLine = "";
            StringTokenizer dimensionVal = null;
            int lineNumber = 0, tokenNumber = 0;
            while( (strLine = br.readLine()) != null)
            {
                    dimensionVal = new StringTokenizer(strLine, ",");
                    dimensionList = new ArrayList<Double>();
                    while(dimensionVal.hasMoreTokens())
                    {
                        tokenNumber++;
                        dimensionList.add(Double.parseDouble(dimensionVal.nextToken()));

                        if(tokenNumber == 3)
                        {
                            Comparator<Double> comparator = Collections.reverseOrder();
                            Collections.sort(dimensionList,comparator);

                        }
                    }
                    listOfDimension.add(dimensionList);
                    tokenNumber = 0;
                lineNumber++;
            }
        }catch (Exception e) {
            throw new Exception();
        }
        finally
        {
            if(br != null)
                br.close();         
        }       
        return listOfDimension;
    }

    public static void main(String[] args) throws Exception {
        System.out.println(getDimensionList("dimension_details.csv"));
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM