已知:
D:wattor\arp0.txt中有一批数据(内容见下)求:
把提取的数据放入 F:shine\arp1.txt1 用JAVA语言可运行的语句;
2 提取:时间,第一个MAC1,第二个MAC2,第一个IP1,第二个IP2----只要这五个数据,其他数据舍去。
举例:
D:lv\arp0.txt
22:51:46.132995 00:0c:f7:fb:02:18 (oui Unknown) > Broadcast, ethertype ARP (0x0806), length 60: arp who-has 219.231.1.78 tell 219.231.1.254
22:51:46.133011 00:0c:f7:fb:02:18 (oui Unknown) > Broadcast, ethertype ARP (0x0806), length 60: arp who-has 219.231.1.23 tell 219.231.1.254
22:51:50.132982 00:0c:f7:fb:02:18 (oui Unknown) > Broadcast, ethertype ARP (0x0806), length 60: arp who-has 219.231.1.78 tell 219.231.1.254
22:51:51.167636 00:1b:b9:d9:a6:98 (oui Unknown) > 00:0c:f7:fb:02:1c (oui Unknown), ethertype ARP (0x0806), length 42: arp who-has 10.2.105.254 tell 10.2.105.25
22:51:51.167902 00:0c:f7:fb:02:1c (oui Unknown) > 00:1b:b9:d9:a6:98 (oui Unknown), ethertype ARP (0x0806), length 60: arp reply 10.2.105.254 is-at 00:0c:f7:fb:02:1c (oui Unknown)
22:51:54.133034 00:0c:f7:fb:02:18 (oui Unknown) > Broadcast, ethertype ARP (0x0806), length 60: arp who-has 219.231.1.78 tell 219.231.1.254F:shine\arp1.txt选取时间,第一个MAC1,第二个MAC2,第一个IP1,第二个IP2----只要这五个数据
    时间              第一个 MAC1        第二个MAC2           第一个IP1             第二个IP2
22:51:46.132995   00:0c:f7:fb:02:18    Broadcast             219.231.1.78          219.231.1.254
22:51:46.133011   00:0c:f7:fb:02:18    Broadcast             219.231.1.23          219.231.1.254
22:51:50.132982   00:0c:f7:fb:02:18    Broadcast             219.231.1.78          219.231.1.254
22:51:51.167636   00:1b:b9:d9:a6:98    00:0c:f7:fb:02:1c     10.2.105.254          10.2.105.25
22:51:51.167902   00:0c:f7:fb:02:1c    00:1b:b9:d9:a6:98     10.2.105.254          
22:51:54.133034   00:0c:f7:fb:02:18    Broadcast             219.231.1.78          219.231.1.254

解决方案 »

  1.   

    这个好像没有特别巧的办法
    偶的笨办法:
    给个简单的思路
    1.前2个字段: 22:51:46.132995   00:0c:f7:fb:02:18 
    直接返回字段分隔符(我不知道是空格还是tab,楼主应该知道)位置,可能是用indexof,再截取子串
    2.第三个字段:Broadcast / 00:0c:f7:fb:02:1c
    这里获取第一次'<'出现的位置,再截取
    3.最后两个字段:
    先截取'length'后面的那部分,再对这部分返回'who-has' or 'tell' or 'reply'位置,比较繁琐,敬请见谅。。
      

  2.   

    根据所提供的信息,写了一个,能完全提取出数据
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Test1 {
        public static void main(String[] args) throws IOException {
            FileAnalyzer analyzer = new FileAnalyzer("arp0.txt", "arp1.txt");
            List<DataBean> beans = analyzer.analyze();
            analyzer.writeData(beans);
            System.out.println("ok!");
        }
    }class FileAnalyzer  {
        private File srcFile = null;
        private File distFile = null;
        private Pattern pattern = Pattern.compile(getPattern());    
        
        public FileAnalyzer(File srcFile, File distFile) {
            this.srcFile = srcFile;
            this.distFile = distFile;
        }    
        public FileAnalyzer(String srcFilename, String distFilename) {
            this(new File(srcFilename), new File(distFilename));
        }    
        public void setPattern(Pattern pattern) {
            this.pattern = pattern;
        }
        
        /**
         * 数据提取
          * @return
         * @throws IOException
         * @author Gao Baowen
         */
        public List<DataBean> analyze() throws IOException {
            List<String> list = readData();
            List<DataBean> beans = new ArrayList<DataBean>();
            for(int i = 0, k = list.size(); i < k; i++) {
                Matcher matcher = pattern.matcher(list.get(i));
                while(matcher.find()) {
                    DataBean bean = new DataBean();
                    bean.setTime(matcher.group(1));
                    bean.setMac1(matcher.group(2));
                    bean.setMac2(matcher.group(3));
                    bean.setIp1(matcher.group(4));
                    bean.setIp2(matcher.group(5));                
                    beans.add(bean);
                }
            }
            return beans;
        }
        
        /**
         * 将提取出来的数据写入文件中
          * @param beans
         * @throws IOException
         * @author Gao Baowen
         */
        public void writeData(List<DataBean> beans) throws IOException {
            BufferedWriter bw = null;
            try {
                bw = new BufferedWriter(new FileWriter(distFile));
                String line = System.getProperty("line.separator");
                for(int i = 0, k = beans.size(); i < k; i++) {
                    if(i > 0){
                        bw.write(line);
                    }
                    bw.write(beans.get(i).toString());
                }
            }finally{
                bw.close();
            }
        }
        
        /**
         * 从文件中读入数据
          * @throws IOException
         * @author Gao Baowen
         */
        private List<String> readData() throws IOException {
            BufferedReader br = null;
            List<String> list = new ArrayList<String>();
            try {
                br = new BufferedReader(new FileReader(srcFile));
                String str = null;
                while( (str = br.readLine()) != null) {
                    str = str.trim();
                    if(str.length() > 0) {
                        list.add(str);
                    }
                }
            }finally{
                br.close();
            }
            return list;
        }
        
        private String getPattern() {
            String pattern = getTimeGroupPattern() + "\\s+" +
                             getMacGroupPattern() + ".+?>\\s+" +
                             getMacGroupPattern() + ".+?" +
                             getIp4GroupPattern() + 
                             "(?:.+?tell\\s+" + getIp4GroupPattern() + ")?";
            return pattern;
        }
        
        private String getTimeGroupPattern() {
            return "(\\d\\d(?::\\d\\d){2}\\.\\d{6})";
        }
        private String getMacGroupPattern() {
            return "(Broadcast|[0-9a-fA-F]{2}(?::[0-9a-fA-F]{2}){5})";        
        }
        private String getIp4GroupPattern() {
            return "(\\d{1,3}(?:\\.\\d{1,3}){3})";
        }
    }class DataBean {
        private String time;
        private String mac1;
        private String mac2;
        private String ip1;
        private String ip2;
        public String getIp1() {
            return ip1;
        }
        public void setIp1(String ip1) {
            this.ip1 = ip1;
        }
        public String getIp2() {
            return ip2;
        }
        public void setIp2(String ip2) {
            this.ip2 = ip2;
        }
        public String getMac1() {
            return mac1;
        }
        public void setMac1(String mac1) {
            this.mac1 = mac1;
        }
        public String getMac2() {
            return mac2;
        }
        public void setMac2(String mac2) {
            this.mac2 = mac2;
        }
        public String getTime() {
            return time;
        }
        public void setTime(String time) {
            this.time = time;
        }
        public String toString() {
            if(ip2 == null) {
                return String.format("%-15s  %-17s  %-17s  %-15s", time, mac1, mac2, ip1);
            }else{
                return String.format("%-15s  %-17s  %-17s  %-15s  %-15s", time, mac1, mac2, ip1, ip2);
            }
        }
    }