嘿,伙计们,我在十字路口,我有两个不同的arrayLists,不同的长度,不同的值。我试图找到两者之间的任何共同值,并将它们输出到一个名为"output.txt“的输出文本文件中。我知道这两个数组列表都填充了来自其他文本文件的值,但是我有权使用的代码不仅仅是生成一个空的输出文本文件,而且我不能理解为什么程序没有将重复的值输出到文本文件"output.txt“中。
下面是我现在使用的代码:
public static void duplciates(){ //compares the two arrayLists dictionary and phoneWords and adds the duplicates to the duplicates arraylist
        for(String term: dictionary){
            if(phoneWords.contains(term)){
                duplicates.add(term);
            }
        }
    }
public static void openOutputFile (){
        try{
            writer = new FileWriter("E:\\output.txt");
        }
        catch(Exception e){
            System.out.println("you have an error");
        }
    }
    public static void writeArrayToFile(){
        for(String str: duplicates){
            try {
                writer.write(str);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }如果你们对错误的地方有任何想法,请让我知道,o如果你们有更好的想法来比较两个数组列表的重复值,请让我知道!
发布于 2014-12-02 12:14:59
在写完之后尝试writer.close()。
发布于 2014-12-02 12:21:34
在writeArrayToFile方法中的for循环之后关闭编写器就可以完成这项工作。
https://stackoverflow.com/questions/27241713
复制相似问题