Java-读取文件并根据文件行属性拆分为多个文件

时间:2019-07-10 05:25:08

标签: java

在此问题中,我们被要求从TCP探针中获取数据并根据IP地址拆分数据。文件的每一行都应逐行读取。如何获取该数据并根据该行中的属性之一进行拆分?同样,这些输入和三个分割文件的名称也必须从命令行参数中提供。该命令应该看起来像这样java Process_TCPProbe_yourinitial tcpprobe.dat tcpprobe_xxxxx_comma.dat tcpprobe_yyyy y_comma.dat tcpprobe_zzzzz_comma.dat

我尝试遍历行并检查行的数组,然后将结果打印到三个文件中,但这没有用。

try{
        BufferedReader br=new BufferedReader(new FileReader(new File(home + File.separator + "Documents" + File.separator + "CS4121" + File.separator + "GENI" + File.separator + "Lab5"+ File.separator +file)));
        BufferedWriter bw1=new BufferedWriter(new FileWriter(new File(home + File.separator + "Documents" + File.separator + "CS4121" + File.separator + "GENI" + File.separator + "Lab5"+ File.separator+file1)));
        BufferedWriter bw2=new BufferedWriter(new FileWriter(new File(home + File.separator + "Documents" + File.separator + "CS4121" + File.separator + "GENI" + File.separator + "Lab5"+ File.separator+file2)));
        BufferedWriter bw3=new BufferedWriter(new FileWriter(new File(home + File.separator + "Documents" + File.separator + "CS4121" + File.separator + "GENI" + File.separator + "Lab5"+ File.separator+file3)));
        String line="";
        while((line=br.readLine())!=null){
            String arr[]=line.split(" ");
            if(arr.length==11){
                String part1=arr[0];
                String ip=arr[1];
                String part2=arr[2];
                String part3=arr[3];
                String part4=arr[4];
                String part5=arr[5];
                String part6=arr[6];
                String part7=arr[7];
                String part8=arr[8];
                String part9=arr[9];
                String part10=arr[10];
                System.out.println(part1+","+ip+","+part2+","+part3+","+part4+","+part5+","+part6+","+part7+","+part8+","+part9+","+part10);
                if (ip == "10.10.1.2:51545"){
                    bw1.write(part1+","+ip+","+part2+","+part3+","+part4+","+part5+","+part6+","+part7+","+part8+","+part9+","+part10);
                    bw1.newLine();
                }
                if (ip == "10.10.1.2:51546"){
                    bw2.write(part1+","+ip+","+part2+","+part3+","+part4+","+part5+","+part6+","+part7+","+part8+","+part9+","+part10);
                    bw2.newLine();
                }
                if (ip == "10.10.1.2:51547"){
                    bw3.write(part1+","+ip+","+part2+","+part3+","+part4+","+part5+","+part6+","+part7+","+part8+","+part9+","+part10);
                    bw3.newLine();
                }
            }

所有显示的文件都是一行,其中没有任何内容,但是当我运行命令时,终端显示正确的输出。输出应该看起来像这样

93.324970324,10.10.1.2:51205,10.10.2.2:5001,32,0x72e71882,0x72e6a762,20,15,341760,165 ,29312 93.337148466,10.10.1.2:51205,10.10.2.2:5001,32,0x72e71882,0x72e6ad0a,20,15,341760,165 ,29312 93.349120821,10.10.1.2:51205,10.10.2.2:5001,32,0x72e723d2,0x72e6b2b2,20,15,341760,166 ,29312

0 个答案:

没有答案