如何获取java中读取CSV文件返回的2d数组的具体行
这是data.csv文件,现在我希望行具有类类型x(任意数量),并将这些extarcted行存储到新数组中,因此如果我有n个类类型,那么我将有n个新数组如何获取java中读取CSV文件返回的2d数组的具体行,java,Java,这是data.csv文件,现在我希望行具有类类型x(任意数量),并将这些extarcted行存储到新数组中,因此如果我有n个类类型,那么我将有n个新数组 age sex zipcode classtype 21 m 23423 1 12 f 23133 2 23 m 32323 2 23 f 23211 1 示例:如果我想检索类类型为1的行,并将这些值存储在新的2d数组中。然后输出应如下所示: array
age sex zipcode classtype
21 m 23423 1
12 f 23133 2
23 m 32323 2
23 f 23211 1
示例:如果我想检索类类型为1的行,并将这些值存储在新的2d数组中。然后输出应如下所示:
array1={{21,m,23423,1},{23,f,23211,1}}
我已经编写了以下代码,将arrayList作为输出
public class CsvParser {
public static void main(String[] args) {
try {
FileReader fr = new FileReader((args.length > 0) ? args[0] : "data.csv");
Map<String, List<String>> values = parseCsv(fr, "\\s,", true);
System.out.println(values);
} catch (IOException e) {
e.printStackTrace();
}
}
public static Map<String, List<String>> parseCsv(Reader reader, String separator, boolean hasHeader) throws IOException {
Map<String, List<String>> values = new LinkedHashMap<String, List<String>>();
List<String> columnNames = new LinkedList<String>();
BufferedReader br = null;
br = new BufferedReader(reader);
String line;
int numLines = 0;
while ((line = br.readLine()) != null) {
if (StringUtils.isNotBlank(line)) {
if (!line.startsWith("#")) {
String[] tokens = line.split(separator);
if (tokens != null) {
for (int i = 0; i < tokens.length; ++i) {
if (numLines == 0) {
columnNames.add(hasHeader ? tokens[i] : ("row_"+i));
} else {
List<String> column = values.get(columnNames.get(i));
if (column == null) {
column = new LinkedList<String>();
}
column.add(tokens[i]);
values.put(columnNames.get(i), column);
}
}
}
++numLines;
}
}
}
return values;
}
我得到的链接很少,上面说的是在“java收集器类”中对元素进行分组,但不知道这是否有用
您的帮助将非常有用。您可以尝试以下方法
String[][] allArrays = new String[50][]; //Set it to however many you need
String classType = "1";
int counter = 0;
Scanner s = new Scanner(new File(fileName));
while(s.hasNextLine()) {
String row = s.nextLine();
if (row.endsWith(classType) {
allArrays[counter++] = row.split(","); //Adds the row, with each element being split by the comma
}
}
不要重新发明轮子,您可以使用现有库将CSV文件的内容转储到Java集合。我通常将CSV文件的内容转储到
列表
。它有一个一行代码来读取所有内容
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
List<String[]> lines= reader.readAll();
CSVReader reader=newcsvreader(newfilereader(“yourfile.csv”);
列表行=reader.readAll();
然后像这样迭代列表以进行分组
Map<String, List<String[]>> values = new LinkedHashMap<String, List<String[]>>();
for(String[] line : lines){
String key = line[4];
if(values.get(key) == null){
values.put(key, new ArrayList<String[]>());
}
values.get(key).add(line);
}
System.out.println(values);
Map values=newlinkedhashmap();
for(字符串[]行:行){
字符串键=行[4];
if(values.get(key)==null){
value.put(key,new ArrayList());
}
获取(键)、添加(行);
}
System.out.println(值);
它给出了一个错误,readAll(),找不到get和put方法,也给出了一个错误,比如类CSVReader中的构造函数CSVReader不能应用于给定类型;必填项:未找到参数:FileReader原因:实际参数列表和正式参数列表长度不同,还有一个问题,m是如何在此处为采购服务的。感谢您的回复@one马铃薯m
是您现有代码中的值。已修改。值
应为映射
而非映射
,因为1个键(类类型)与多个记录(列表
)关联
Map<String, List<String[]>> values = new LinkedHashMap<String, List<String[]>>();
for(String[] line : lines){
String key = line[4];
if(values.get(key) == null){
values.put(key, new ArrayList<String[]>());
}
values.get(key).add(line);
}
System.out.println(values);