我在本地存储中有一些数据的文本文件。现在,我只需要为选定的行数据更新数据。
This is my textfile data
Kumar
Gaaa
9394769487
Male
Un Married
Tue Jun 12 00:00:00 IST 1984
kumar@gmail.com
Kumar@1993
rtyhrty
India
在这里我从不同的领域接收新数据。所以我需要更新 特定数据行到相关数据 例如:第一行是Name = kumar, 我想更新Name = ajay
这是我的阅读逻辑,并将所有值放入HashMap对象。 您能告诉我还有其他流程可用于将所有数据行放入Map对象中的特定键吗?
String temp;
String[] line=new String[15];
InputStream stream=new FileInputStream(path);
BufferedReader reader =new BufferedReader(new InputStreamReader(stream));
while((temp = reader.readLine()) != null)
{
for(int j=0; j<line.length ;j++)
{
line[j]=reader.readLine();
String fname=line[0];
String lname=line[1];
String mobile=line[2];
String gender=line[3];
String marry=line[4];
String dob=line[5];
String email=line[6];
String pwd=line[7];
String address=line[8];
String country=line[9];
String idcard=line[10];
String idfile=line[11];
String addcard=line[12];
String addfile=line[13];
map.put("fname",fname);
map.put("lname",lname);
map.put("mobile",mobile);
map.put("gender",gender);
map.put("marry",marry);
//map.put("dob",dob);
map.put("email",email);
map.put("address",address);
map.put("country",country);
map.put("idcard",idcard);
map.put("idfile",idfile);
map.put("addcard",addcard);
map.put("addfile",addfile);
}
}
stream.close();
reader.close();
答案 0 :(得分:0)
我建议逐行读取文件,更改需要更改的行并将所有行写入新文件,然后删除旧文件并重命名新文件。除非文件很大,否则您只需要更改前几行即可。
答案 1 :(得分:0)
答案 2 :(得分:0)
在以下情况下,您可以使用地图或逐行阅读都是有效的
希望这会有所帮助!