how to edit my compare method

守給你的承諾、 提交于 2019-12-20 06:00:16

问题


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. Also I dont have an error for coding

I don't have a result. here is my code


回答1:


I just ran your program with the following files and could not reproduce your problem.

deneme1

abc
def
ghi

deneme2

abc
ghi
klm

And deneme3 was created with the following content:

abc
ghi

EDIT

It seems you want the opposite behaviour. Some of your methods are unnecessarily complicated and could be made much shorter by using the right tools of the standard JDK. See below an example of a simplified implementation (that only keeps the words that are not in common between the 2 files) - this example is case sensitive:

public class TextAreaSample {

    public static void main(String[] args) throws IOException {
        //readAllLines does what you do in readFileAsList
        List<String> strings1 = Files.readAllLines(Paths.get("C:/temp/deneme1.txt"), Charset.defaultCharset());
        List<String> strings2 = Files.readAllLines(Paths.get("C:\\temp\\deneme2.txt"), Charset.defaultCharset());
        Set<String> notInCommon = getNotInCommon(strings1, strings2);
        write(notInCommon, "C:\\temp\\deneme3.txt");
    }

    private static void write(Collection<String> out, String fname) throws IOException {
        FileWriter writer = new FileWriter(new File("C:\\temp\\deneme3.txt"));
        for (String s : out) {
            writer.write(s + "\n");
        }
        writer.close();
    }

    private static Set<String> getNotInCommon(List<String> strings1, List<String> strings2) {
        //Sets are great to get unique lists and check commonality
        Set<String> onlyInFile1 = new HashSet<String>(strings1);
        onlyInFile1.removeAll(strings2); //remove strings in s1 AND s2
        Set<String> onlyInFile2 = new HashSet<String>(strings2);
        onlyInFile2.removeAll(strings1); //remove strings in s1 AND s2
        Set<String> notInCommon = new HashSet<>();
        notInCommon.addAll(onlyInFile1);
        notInCommon.addAll(onlyInFile2);

        return notInCommon;
    }
}



回答2:


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<String> strings1, List<String> strings2)
  throws IOException
  {
    final Collator c = Collator.getInstance();
    c.setStrength(Collator.PRIMARY);
    final SortedSet<String>
      union = new TreeSet<String>(c),
      intersection = new TreeSet<String>(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<String> 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<String> readFileAsList(String name) throws IOException {
    final List<String> ret = new ArrayList<String>();
    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



回答3:


My suggestion is don't try to solve everything in one shot. You can simplify your compare method by using one liner strings1.retainAll(strings2)

See this for more info http://docs.oracle.com/javase/6/docs/api/java/util/Collection.html#retainAll(java.util.Collection)

And print the contents of the strings1 and see if it is okay then solve that file writing part.




回答4:


You are opening the third file deneme3.txt twice without closing it in between. I guess the second time (in write()) an exception will be thrown, so there will be no write. Remove the first occurence of FileWriter writer = new FileWriter(new File("D:\\Denemeler\\deneme3.txt")); (the one in compare()) and you should be fine.




回答5:


I think you have to flush() your writer before closing it.

private static void write(ArrayList<String> out, String fname) throws IOException {
    FileWriter writer = new FileWriter(new File("D:\\Denemeler\\deneme3.txt"));
    for (int i = 0; i < out.size(); i++) {
        writer.write(out.get(i) + "\n");
    }
    // Flush the writer before closing it.
    writer.flush();

    writer.close();
}


来源:https://stackoverflow.com/questions/11792534/how-to-edit-my-compare-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!