思路:
按行读取数据,将有用的行提取出来,然后从行里提取有用得数据保存在数组中。
readLine()方法是由BufferedReader类提供的阻塞式方法,当没有数据读取时,就一直会阻塞在那,而不是返回null;
如果不指定buffer大小,则readLine()使用的buffer有8192个字符。在达到buffer大小之前,只有遇到”/r”、”/n”、”/r/n”才会返回
package readText; import java.util.List; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.LinkedList; public class Compare { private String date; private String missionid; private String com; private String rfid1; private String rfid2; List<Compare> a=new LinkedList<Compare>(); public Compare(String date,String missionid,String com,String rfid1,String rfid2){ this.date=date; this.missionid=missionid; this.com=com; this.rfid1=rfid1; this.rfid2=rfid2; } public Compare(){} public void setGh(String date){ this.date=date; } public String getDate(){ return this.date; } public void setShift(String missionid){ this.missionid=missionid; } public String getMissionid(){ return this.missionid; } public void setCom(String com){ this.com=com; } public String getCom(){ return this.com; } public void setRfid1(String rfid1){ this.rfid1=rfid1; } public String getRfid1(){ return this.rfid1; } public void setRfid2(String rfid2){ this.rfid2=rfid2; } public String getRfid2(){ return this.rfid2; } }
package readText; import java.io.BufferedReader; import java.io.FileReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.LinkedList; import readText.Compare; public class ReadData { public static void main(String[] args) throws IOException { try{ String dir="Log_2020-12-06_000"; BufferedReader in = new BufferedReader(new FileReader("C:\机器人Log\05\"+dir+".txt"));//打开文件创建数据流 BufferedWriter out = new BufferedWriter(new FileWriter("C:\机器人Log\05\"+dir+"_out.txt")); BufferedWriter out_rfid = new BufferedWriter(new FileWriter("C:\机器人Log\05\"+dir+"_out_RFID.txt")); String str; //定义String变量用来保存每一次读到的每一行的数据 int i=0;//行数 int j=0;//RFID个数 int temp=0; String Date=""; String Missionid=""; String com=""; String rfid1=""; String rfid2=""; ArrayList<String> arraylist =new ArrayList<String>(); List<Compare> ls=new LinkedList<Compare>(); /*对下面的数据进行筛选*/ while((str=in.readLine())!=null){ if(str.contains("MissionID")&&str.contains("Command")&&str.contains("RFID")){//筛选条件 if(str.length()>0){ arraylist.add(str); if(str.contains("PSwapD")){ j=j+2; Date=str.substring(0, 19); Missionid=str.substring(43, 58); com="PSwapD"; rfid1=str.substring(112, 120); rfid2=str.substring(145, 154); //System.out.println(Date+Missionid+com+rfid1+rfid2); Compare compare=new Compare(Date,Missionid,com,rfid1,rfid2); //System.out.println(compare.getDate()); ls.add(compare); }else{ temp=temp+1; j=j+1; if(str.contains("PD")){ Date=str.substring(0, 19); Missionid=str.substring(43, 58); com="PD"; rfid1=str.substring(120, 128); rfid2=""; //System.out.println(Date+Missionid+com+rfid1+rfid2); Compare compare=new Compare(Date,Missionid,com,rfid1,rfid2); //System.out.println(compare.getDate()); ls.add(compare); } if(str.contains("Pickup")){ Date=str.substring(0, 19); Missionid=str.substring(43, 58); com="Pickup"; rfid1=str.substring(113, 122); rfid2=""; //System.out.println(Date+Missionid+com+rfid1+rfid2); Compare compare=new Compare(Date,Missionid,com,rfid1,rfid2); //System.out.println(compare.getDate()); ls.add(compare); }if(str.contains("Dropoff")){ Date=str.substring(0, 19); Missionid=str.substring(43, 58); com="Dropoff"; rfid1=str.substring(112, 120); rfid2=""; //System.out.println(Date+Missionid+com+rfid1+rfid2); Compare compare=new Compare(Date,Missionid,com,rfid1,rfid2); //System.out.println(compare.getDate()); ls.add(compare); } } i=i+1; } } } in.close(); for (String s: arraylist){ out.write(s); out.newLine(); out.flush(); } System.out.println(ls.size()); for(int z=0;z<ls.size();z++){ String rfid=ls.get(z).getDate()+" "+ls.get(z).getMissionid()+" "+ls.get(z).getCom()+" "+ls.get(z).getRfid1()+" "+ls.get(z).getRfid2(); System.out.println(rfid); out_rfid.write(rfid); out_rfid.newLine(); out_rfid.flush(); } out.write("输出完毕,共"+i+"行,"+j+"RFID"); out.close(); out_rfid.close(); System.out.println("输出完毕,共"+i+"行"); System.out.println("输出完毕,共"+j+"个RFID"); System.out.println("输出完毕,共"+temp+"个RFID"); } catch (IOException e) { } } }