现在有个问题,我有一个文本,上面的数据不全,需要将另外一个文本的数据插入,以id号的顺序插入
上面的是不全的文本,下面的是需要插入的文本
最后是这样的,如果第一个文本,即不全的文本数据量比较大,还需要用上面方式来处理?
java将一个文本的数据插入另一个文本的指定位置
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
8条回答 默认 最新
关注 package design; import org.yaml.snakeyaml.reader.UnicodeReader; import java.io.*; import java.util.*; public class ReadTxt { public static List<Map<String,String>> readFromLockingTxt(String fileName) { BufferedReader reader = null; List<Map<String,String>> mapList = new ArrayList<Map<String,String>>(); try{ FileInputStream fileInputStream = new FileInputStream(new File(fileName)); UnicodeReader unicodeReader = new UnicodeReader(fileInputStream); reader = new BufferedReader(unicodeReader); String tempString = null; int line = 1; while ((tempString = reader.readLine()) != null) { Map<String,String> map = new LinkedHashMap<String,String>(); System.out.println("line " + line + ": " + tempString); tempString.trim(); if(line==1){ String [] strs = tempString.split("\\|"); map.put("id",strs[0].trim()); map.put("name",strs[1].trim()); map.put("pass",strs[2].trim()); }else{ //替换多个空格 String string = tempString.replaceAll(" {1,}", ""); String [] strs = string.split(","); map.put("id",strs[0].trim()); map.put("name",strs[1].trim()); if(strs.length == 3){ map.put("pass",strs[2].trim()); }else{ map.put("pass",""); } } mapList.add(map); line++; } reader.close(); }catch (IOException e){ e.printStackTrace(); }finally { if (reader != null){ try{ reader.close(); }catch (IOException e1){ e1.printStackTrace(); } } } return mapList; } public static Map<String,String> readFromKeyTxt(String fileName) { BufferedReader reader = null; Map<String,String> map = new LinkedHashMap<String,String>(); try{ FileInputStream fileInputStream = new FileInputStream(new File(fileName)); UnicodeReader unicodeReader = new UnicodeReader(fileInputStream); reader = new BufferedReader(unicodeReader); String tempString = null; int line = 1; while ((tempString = reader.readLine()) != null) { System.out.println("line " + line + ": " + tempString); tempString.trim(); //替换多个空格 String string = tempString.replaceAll(" {1,}", ""); String [] strs = string.split(","); map.put(strs[0].trim(),strs[1].trim()); line++; } reader.close(); }catch (IOException e){ e.printStackTrace(); }finally { if (reader != null){ try{ reader.close(); }catch (IOException e1){ e1.printStackTrace(); } } } return map; } public static void writeListToTxt(List<Map<String,String>> list,String fileName){ StringBuilder sb = new StringBuilder(); for(int i=0;i<list.size();i++){ if(i==0){ sb.append(list.get(i).get("id")+"a\u007C"+list.get(i).get("name")+"a\u007C"+list.get(i).get("pass")).append("\r\n"); }else{ sb.append(list.get(i).get("id")+","+list.get(i).get("name")+","+list.get(i).get("pass")).append("\r\n"); } } try{ FileWriter writer = new FileWriter(fileName); BufferedWriter bw = new BufferedWriter(writer); bw.write(sb.toString()); bw.close(); writer.close(); }catch(Exception e){ e.printStackTrace(); } } public static boolean isEmpty(String s) { return ((s == null) || (s.length() == 0)); } }
package design; import java.io.File; import java.util.List; import java.util.Map; public class App { public static void main(String args[]){ //读取缺失的文件 List<Map<String,String>> lockMapList = ReadTxt.readFromLockingTxt("D:"+ File.separator+"workspace"+File.separator+"lacking.txt"); System.out.println("修改前list为:"+lockMapList); //读取补充文件 Map<String,String> keyMap = ReadTxt.readFromKeyTxt("D:"+ File.separator+"workspace"+File.separator+"key.txt"); System.out.println("需要插入的文本为:"+keyMap); //组合成新的完整的list lockMapList.parallelStream().forEach(p->{ if(ReadTxt.isEmpty(p.get("pass"))){ p.replace("pass",keyMap.get(p.get("id"))); } }); System.out.println("组合后list为:"+lockMapList); //重组完成后的List存储到txt文件 ReadTxt.writeListToTxt(lockMapList,"D:"+ File.separator+"workspace"+File.separator+"whole.txt"); } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 用verilog实现tanh函数和softplus函数
- ¥15 求京东批量付款能替代天诚
- ¥15 slaris 系统断电后,重新开机后一直自动重启
- ¥15 51寻迹小车定点寻迹
- ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
- ¥15 关于vue2中methods使用call修改this指向的问题
- ¥15 idea自动补全键位冲突
- ¥15 请教一下写代码,代码好难
- ¥15 iis10中如何阻止别人网站重定向到我的网站
- ¥15 滑块验证码移动速度不一致问题