import org.apache.commons.lang3.StringUtils; import java.io.*; import java.lang.reflect.Array; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; public class Test { public static void main(String[] args) throws IOException { FileInputStream fileInputStream = new FileInputStream(new File("src/data.txt")); byte[] bbuf = new byte[1024]; //用于保存实际读取的字节数 int hasRead = 0; //使用循环来重复读取数据 String content = ""; while( (hasRead = fileInputStream.read(bbuf)) > 0){ //将字节数组转换为字符串输出 //// ////System.out.println("================="); //System.out.print(new String(bbuf,0,hasRead)); content += new String(bbuf,0,hasRead); } String[] line = content.split("\n"); // ////System.out.println(line.length); Map<String , Integer> map = new HashMap<>(); List<String> list = new LinkedList<>(); for (int i = 0; i < line.length; i++) { // ////System.out.println("第"+i+"行:"+line[i]); String lineStr = line[i]; String[] nums = lineStr.trim().split(" "); // ////System.out.println(nums); if (nums.length<=2){ ////System.out.println(lineStr); list.add(lineStr); }else{ List<String> appendList = new LinkedList<>(); for (int i1 = 0; i1 < nums.length; i1++) { int getNum = Integer.parseInt(nums[i1]); int result = getNum; if(getNum>10000){ if (map.get(getNum+"")==null){ map.put(getNum+"",getNum); }else{ int getMapNum = map.get(getNum+""); result = getMapNum+1; map.put(getNum+"",result); } } // //System.out.println(result); appendList.add(result+""); } list.add(StringUtils.join(appendList.toArray(), " ") ); } } BufferedWriter out = new BufferedWriter(new FileWriter("src/result.txt")); int index = 0 ; String all = ""; //System.out.println(list.size()); Map<String,String> map1 = new HashMap<>(); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); String lineStr = list.get(i); String[] nums = lineStr.trim().split(" "); for (int i1 = 0; i1 < nums.length-1; i1++) { //System.out.println("test::::"+nums[i1]+" "+nums[i1+1]); if(map1.get(nums[i1]+" "+nums[i1+1])==null){ index++; all+=nums[i1]+" "+nums[i1+1]+"\n"; } map1.put(nums[i1]+" "+nums[i1+1],nums[i1]+" "+nums[i1+1]+"\n"); } } //System.out.println("2341234123421341234"); //System.out.println(all); out.write(all); out.close(); //把 //System.out.println(line); //改成bai //System.out.println(index); //关闭文件输出流,放在finally块里更安全 fileInputStream.close(); } }
最近下载更多