读取第一行的java文本文件具有键和第二行前进值
****文本文件格式:****
FirstName,lastname,role,startdate,emptype
sreedhar,reddy,Admin,20-2-2018,contract
shekar,kumar,Admin,20-2-2018,contract
RAJ,roy,Admin,20-2-2018,contract
somu,reddy,Admin,20-2-2018,contract
sumanth,reddy,Admin,20-2-2018,contract
问题:
如何读取文本文件以及如何输入地图(键、值)
第一行在映射中有键(例如:firstname、lastname等)
地图中向前值的第二行(例如:sreedhar、reddy等)
映射输出:{Firstname:sreedhar,Lastname:reddy,role:Admin,startdat:2-6-2018}
请任何一个提供java代码的读取文本文件,并放入地图读取有键,值对
# 1 楼答案
您需要为Map指定不同的键,因为它每次都需要一个唯一的键:
因此,您很可能需要
Map
个Map
s:在文件中读取:
添加到扫描仪:
将第一行读作“标题”:
创建一个
Map
的Map
:循环遍历文本文件的其余部分,添加到
Map
,然后将Map
添加到主Map
,以及一个键(我使用了User0, User1...
):一旦你把它分解成步骤,生活就会变得更轻松
# 2 楼答案
你可以这样做-
输出: