【发布时间】:2018-10-31 05:51:54
【问题描述】:
我尝试从 csv 文件中读取数据,将其保存到linkedHashMap 并打印出来。但问题是我需要分别打印键和值。 CSV 文件只有 2 列:第一:电子邮件,第二:名称。
public class CsvReader {
String CSVPath = "c:/Users/PC/Desktop/file.csv";
CSVReader reader;
public void readCsvFile() throws IOException {
try {
reader = new CSVReader(new FileReader(CSVPath));
String[] column;
ArrayList<LinkedHashMap<String, String>> myArraylist = new
ArrayList<LinkedHashMap<String, String>>();
LinkedHashMap<String, String> map;
while ((column = reader.readNext()) != null) {
map = new LinkedHashMap<String, String>();
map.put("Emails", column[0]);
myArraylist.add(map);
}
reader.close();
for (int i = 0; i < myArraylist.size(); i++) {
System.out.println(myArraylist.get(i).get("Emails").toString());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
当我运行这段代码时,我得到以下信息:
info@staybysantacruz.com;Quality Inn & Suites Santa Cruz Mountains
VacationRentals321@gmail.com;In Big Bear
info@haiyi-hotels.com;Best Western Americania
所以它会同时打印电子邮件和姓名。 我试图获取linkedHashMap 的键和值,但没有运气。 有人可以帮我吗?
【问题讨论】:
-
数据最后应该是什么样子?
-
你能分享.cvs文件吗?
-
CSV 文件在这里:ufile.io/43d2t。它没有任何分号。
-
基本上我需要将日期保存到 LinkedHAshMap,然后才能打印出 Key 或 Value。但不能同时使用 Key + Value。
标签: java csv linked-list readfile linkedhashmap