比较两个不同文本文件中的行并修改d目标行的最后一列

时间:2015-11-05 13:42:44

标签: java

我是一个java初学者......我想使用文件2中每行的第一列作为标准来检查文件1中是否存在这样的行...如果它确实改变了最后一列(是的,没有)在文件1中的'可能'这样的行。

File 1 
6130124,860847,9,9,4,9,e,6,2,S, yes
6150744,194559,7,7,4,9,e,6,2,S,no
6065543,511353,5,4,4,9,e,6,2,S,no
8419554,261221,6,6,2,18,a,7,1,S,yes
8190063,144544,5,5,2,18,a,7,1,S,no
8276868,285387,4,4,2,18,a,7,1,S,no
625541,233528,2,2,4,9,a,3,1,N,yes
676477,138280,2,2,4,9,a,3,1,N,no
628496,59404,2,2,4,9,a,3,1,N,no

File 2 ( a subset of File 1)
6130124,860847,9,9,4,9,e,6,2,S, yes
6150744,194559,7,7,4,9,e,6,2,S,no
8276868,285387,4,4,2,18,a,7,1,S,no
625541,233528,2,2,4,9,a,3,1,N,yes

这意味着......文件1将打印为:

6130124,860847,9,9,4,9,e,6,2,S, maybe
6150744,194559,7,7,4,9,e,6,2,S, maybe
6065543,511353,5,4,4,9,e,6,2,S,no
8419554,261221,6,6,2,18,a,7,1,S,yes
8190063,144544,5,5,2,18,a,7,1,S,no
8276868,285387,4,4,2,18,a,7,1,S,maybe
625541,233528,2,2,4,9,a,3,1,N,mabe
676477,138280,2,2,4,9,a,3,1,N,no
628496,59404,2,2,4,9,a,3,1,N,no

这是我到目前为止所做的事情

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;



public class ComparingBothFiles {


    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        PrintWriter out = null;
        BufferedReader br = null;
        BufferedReader br2 = null;
        String temp = null;
        try {
            br = new BufferedReader(new FileReader("File2.txt")); 

            out = new PrintWriter(new BufferedWriter(new FileWriter(
                            "Me.txt", true))); //the file you write in

            String line = br.readLine();

            while (!line.isEmpty()) {
                String ID = line.split(",")[0];
                if (!ID.equals(temp)) {
                    try {
                        br2 = new BufferedReader(
                                new FileReader("File1.txt")); //the proper database
                        String line2 = br2.readLine();
                        while (!line2.isEmpty()) {
                            String ID2 = line2.split(",")[0];
                            if (ID2.equals(ID)) {
                                out.println(line2);
                                //erase the line you've just written if you want
                            }
                            line2 = br2.readLine();
                        }
                        br2.close();
                    } catch (Exception e) {
                        System.out.println("error");
                    } finally {
                        if (br2 != null) {
                            br2.close();
                        }
                    }
                }
                line = br.readLine();
                temp = ID;
            }


            out.close();
        } catch (FileNotFoundException e) {
            System.out.println("FileNotFound");
        } catch (NullPointerException e) {
            System.out.println("null");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("error");

        } finally {
            if (out != null) {
                out.close();
            }
            if (br != null) {
                br.close();
            }
        }

    }

1 个答案:

答案 0 :(得分:1)

第一件事建议br.readLine()在到达文件末尾时返回null,所以

String line = br.readLine();
while(line != null){
    line = br.breadLine();
}

将您在List<List<String>>;

中阅读的行存储起来

将您的值存储在List<String>;

中的一行中

继承人的工作代码

public class Main {

   public static void main(String[] args) throws
         Exception {
      List<List<String>> firstFileLines = readFileLines("file1");
      List<List<String>> secondFileLines = readFileLines("file2");

      int column = 0;
      for (List<String> line : firstFileLines) {
         String columnValue = line.get(column);
         boolean valueMatch = false;
         for (List<String> secondFileLine : secondFileLines) {
            if (columnValue.equals(secondFileLine.get(column))) {
               valueMatch = true;
               break;
            }
         }
         if (valueMatch) {
            int lastIndex = line.size() - 1;
            line.set(lastIndex, "REPLACE_ME");
         }
         System.out.println(String.join(",", line));
      }
   }

   private static List<List<String>> readFileLines(String file)
         throws Exception {
      BufferedReader br1 = new BufferedReader(new FileReader(file));
      List<List<String>> fileLines = new ArrayList<>();

      String line = br1.readLine();
      while (line != null) {
         List<String> valuesFromLine = new ArrayList<>();
         String[] values = line.split(",");
         for (String val : values) {
            valuesFromLine.add(val.trim());
         }
         fileLines.add(valuesFromLine);

         line = br1.readLine();
      }

      br1.close();
      return fileLines;
   }
}

继承我复制的文件的输出

6130124,860847,9,9,4,9,e,6,2,S,REPLACE_ME
6150744,194559,7,7,4,9,e,6,2,S,REPLACE_ME
6065543,511353,5,4,4,9,e,6,2,S,no
8419554,261221,6,6,2,18,a,7,1,S,yes
8190063,144544,5,5,2,18,a,7,1,S,no
8276868,285387,4,4,2,18,a,7,1,S,REPLACE_ME
625541,233528,2,2,4,9,a,3,1,N,REPLACE_ME
676477,138280,2,2,4,9,a,3,1,N,no
628496,59404,2,2,4,9,a,3,1,N,no
相关问题