允许arrayList从CSV中接收三列数据



我被困在如何加载的CSV上

试试这个

   public class FileParser  {
 public static ArrayList<String> parseFile(String fileName){
  String csvFile = fileName;
  BufferedReader br = null;
 String line = "";
 final String DELIMITER = ",";
 ArrayList<String> data = new ArrayList<String>();
try {
    int counter = 0;
    int N = 10;
    br = new BufferedReader(new FileReader(csvFile));
    while ((line = br.readLine()) != null && counter < N) {
        if(counter >= 0){
            String dataRow = line;
            data.add(dataRow);
        }
        counter++;
    }
    for (String string : data) {
        string.split(DELIMITER);
        System.out.println(string);
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (br != null) {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
return data;

}}

您可以做的是:

  • 如果第一行有X Y Z,则跳过它
  • 再声明两个ArrayList来存储Y和Z列

你可以这样做:

  public class FileParser  {
    public ArrayList<String> parseFile(String fileName){
    String csvFile = fileName;
    BufferedReader br = null;
    String line = "";
    final String DELIMITER = ",";
    ArrayList<String> dataX = new ArrayList<String>();
    ArrayList<String> dataY = new ArrayList<String>();
    ArrayList<String> dataZ = new ArrayList<String>();
     try {
         int counter = 0;
         int N = 10;
          br = new BufferedReader(new FileReader(csvFile));
          while ((line = br.readLine()) != null && counter < N) {
          if(counter > 0){
            String[] dataRow = line.split(DELIMITER);
            dataX.add(dataRow[0]);
            dataY.add(dataRow[1]);
            dataZ.add(dataRow[2]);
        }
        counter++;
    }
  } catch (FileNotFoundException e) {
    e.printStackTrace();
 } catch (IOException e) {
    e.printStackTrace();
 } finally {
    if (br != null) {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
 return data;
}

我想更好的选择是创建一个包含三个变量x、y和z的自定义类。然后使用这个类对象来存储数据。根据需要对重写的equals和hashCode方法进行实现更改。

    public final class CsvData{
        private String x;
        private String y;
        private String z;
        public CsvData(String x, String y, String z){
            this.x = x;
            this.y = y;
            this.z = z;
        }
        //getters and setters
        public boolean equals(Object o) {
            if (o instanceof CsvData) {
                CsvData node = (CsvData)o;
                return (x.equals(node.x) && y.equals(node.y) && z.equals(node.z));
            }
            return false;
        }
        public int hashCode() {
            return HashCodeBuilder.reflectionHashCode(this);
        }
    } 

public class FileParser  {
public ArrayList<CsvData> parseFile(String fileName){
    String csvFile = fileName;
    BufferedReader br = null;
    String line = "";
    final String DELIMITER = ",";
    ArrayList<CsvData> data = new ArrayList<CsvData>();
    try {
        int counter = 0;
        int N = 10;
        br = new BufferedReader(new FileReader(csvFile));
        while ((line = br.readLine()) != null && counter < N) {
                String[] dataRow = line.split(DELIMITER);
                CsvData csvData = new CsvData(dataRow[0],dataRow[1],dataRow[2]);
                data.add(csvData);
             counter++;
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return data;
  }

最新更新