我是 Java 新手。有人可以向我建议如何读取包含 7 列的 CSV 文件吗?
这是我的代码:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class main {
public static Map<String,String> map1 = null;
public static Map<String,String> map2 = null;
public static void main(String[] args) {
try {
readFileandPopulate();
for (Map.Entry<String, String> entry : map1.entrySet()) {
System.out.println("Key : " + entry.getKey() + " Value : "
+ entry.getValue()+" address :"+map2.get(entry.getKey()));
//insert into DB
}
} catch (Exception e) {//Catch exception if any
e.printStackTrace();
System.err.println("Error: " + e.getMessage());
}
}
public static void readFileandPopulate() throws Exception {
FileInputStream fstream = new FileInputStream("/Users/Desktop/Pattern Recognition/DataSetR/1a19.csv");
// Use DataInputStream to read binary NOT text.
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
map1 = new HashMap<String,String>();
while ((strLine = br.readLine()) != null) {
System.out.println(strLine);
String temp[] = strLine.split(",");
map1.put(temp[0], temp[1]);
}
in.close();
System.out.println("done 1");
}
}
有没有什么方法可以只读取几列并删除剩余的数据,或者我可以将所有值存储在数组中并使用数组的索引来计算一个使用值的方程?