java读文件,文件中包含不同用户,根据用户的不同将数据读到不同文件!求一个简单的java demo!第一个字段代表用户id:
2 回答
Qyouu
TA贡献1786条经验 获得超11个赞
这样?
Map bWriters = new HashMap<>();
try(BufferedReader bReader = new BufferedReader(new FileReader("fileName"))) {
String tStr = null;
while ((tStr = bReader.readLine()) != null){
String[] strs = tStr.split(",");
String usrId = strs[0];
BufferedWriter bWriter = bWriters.get(usrId);
if(bWriter == null){
bWriter = new BufferedWriter(new FileWriter("targetFileName"));
bWriters.put(usrId, bWriter);
}
bWriter.write(tStr);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bWriters!=null){
for(BufferedWriter bw:bWriters.values()){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
添加回答
举报
0/150
提交
取消