how to edit my compare method

前端 未结 5 1543
孤城傲影
孤城傲影 2021-01-27 09:27

I want to compare contens of my two txt files and write the different words in other file3.txt file

I want to do compare method in this way to write another txt file. A

5条回答
  •  失恋的感觉
    2021-01-27 09:59

    I have simplified and corrected your code into this:

    public class TextAreaSample
    {
      public static void main(String [] args) throws IOException {
        compare(readFileAsList("deneme1.txt"),
                readFileAsList("deneme2.txt"));
      }
    
      private static void compare(List strings1, List strings2)
      throws IOException
      {
        final Collator c = Collator.getInstance();
        c.setStrength(Collator.PRIMARY);
        final SortedSet
          union = new TreeSet(c),
          intersection = new TreeSet(c);
        union.addAll(strings1);
        union.addAll(strings2);
        intersection.addAll(strings1);
        intersection.retainAll(strings2);
        union.removeAll(intersection);
        write(union, "deneme3.txt");
      }
    
      private static void write(Collection out, String fname) throws IOException {
        FileWriter writer = new FileWriter(new File(fname));
        try { for (String s : out) writer.write(s + "\n"); }
        finally { writer.close(); }
      }
    
      private static List readFileAsList(String name) throws IOException {
        final List ret = new ArrayList();
        final BufferedReader br = new BufferedReader(new FileReader(name));
        try {
          String strLine;
          while ((strLine = br.readLine()) != null) ret.add(strLine);
          return ret;
        } finally { br.close(); }
      }
    }
    

    I have deneme1.txt:

    plane
    horoscope
    microscope
    

    deneme2.txt:

    phone
    mobile
    plane
    

    Output in deneme3.txt:

    horoscope
    microscope
    mobile
    phone
    

提交回复
热议问题