为什么读取的文件中的行没有添加到我的ArrayList中?

时间:2016-07-06 23:39:00

标签: java arraylist

编写以下代码以获取两个文件,找到不重复的值,并将它们输出到另一个文件。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

//keeps only uniques
//remove all values that are duplicates
public class dupeCleaner {
    public static void main(String[]args){
        String fileA = "Testing1.txt";
        String fileB = "Testing2.txt";
        String[] uniques=clean(fileA,fileB);
        writeFile("file.txt",uniques);
    }
    public static String[] clean(String fileA, String fileB){
        String line;
        ArrayList<String> cleaned = new ArrayList<>();
        String[] list1 = readFile(fileA);
        String[] list2 = readFile(fileB);
        boolean[] firstList = new boolean[list1.length];
        for(int k = 0; k < list1.length;k++){
            firstList[k]=checkDupe(list1[k],list2);
        }
        for(int k = 0; k< list1.length;k++){
            if(firstList[k]){
                cleaned.add(list1[k]);
                System.out.println(list1[k]);
            }
        }
        String[] finalList = new String[cleaned.size()];
        finalList=cleaned.toArray(new String[cleaned.size()]);
        return finalList;
    }
    public static void writeFile(String fileName, String[] list){
        try{
        BufferedWriter bw = new BufferedWriter(new FileWriter(fileName,true));
        for(String k: list){
            bw.write(k+"\r\n");
            bw.close();
        }
        }catch(IOException e){
        }
    }
    public static boolean checkDupe(String var, String[] list){
        for(String k : list){
            if(k.equals(var)){
                System.out.println("true");
                return true;
            }
        }
        System.out.println("false");
        return false;
    }
    public static String[] readFile(String fileName){
        ArrayList<String> vars = new ArrayList<>();
        String line;
        try {
            FileReader fr = new FileReader(fileName);
            BufferedReader br = new BufferedReader(fr);
            line = br.readLine();
            while (line != null) {
                vars.add(line);
                line = br.readLine();
            }
        } catch (IOException e) {

        }
        System.out.println(vars.get(5));
        String[] list = vars.toArray(new String[vars.size()]);
        return list;
    }
}

问题在于静态方法readFile,我不明白为什么System.out.println(vars.get(5));返回一个IndexOutOfBoundsException,所有代码对我来说都是有意义的,并且文件肯定是被读取的(例如,第一个文件中有100行,如果我只输出String行,它们都会在控制台中打印)。

有人能够解释我的问题吗?

2 个答案:

答案 0 :(得分:1)

您需要在 $property = new Property([ 'user_id' => $user->id, 'is_for_sale' => $request['listing_type'], 'available' => $request['available'], 'title' => $request['title'], 'description' => $request['description'], 'price' => $request['price'], ]); $property->save(); //save address and related to property $address = new Address([ 'building_name' => $request['building_name'], 'street_number' => $request['street_number'], 'street_name' => $request['street_name'], 'town' => $request['town'], 'county' => $request['county'], 'postcode' => $request['postcode'], ]); $property->addresses()->save($address); //save property and related to residential $residential = new Residential([ 'furnished' => $request['furnished'], 'rooms' => $request['bedrooms'], 'rent' => $request['price'], ]); $residential_type = new Residential_Types([ 'residential_type' => $request['property_type'], ]); $residential_type->save(); $property->Residential()->save($residential); $residential->Residential_Type()->save($residential_type); return view('renting'); 循环中包含id="eyJjbGFpbSI6IHsidHlwZSI6ICJkb21haW4iLCAicm9sZSI6ICJ1c2VyOk", ts="1467847506", nonce="YTcdzC" 语句。你正在做的是在文件中写一行并关闭id="eyJjbGFpbSI6IHsidHlwZSI6ICJkb21haW4iLCAicm9sZSI6ICJ1c2VyOk", ts="1467851106", nonce="YTcdzC" ,它只会将第一个字符串写入文件。

bw.close

答案 1 :(得分:0)

您的第一个问题是您的代码没有保留唯一值,而是保留重复的代码。您的文本文件可能没有足够的重复项,这就是您获得IndexOutOfBoundsException的原因。将clean()方法中的条件更改为:

if (!firstList[k])

您遇到的第二个问题是您尝试写入已关闭的文件,因为您在foreach循环中将其关闭。从bw.close()中的循环中删除writeFile()语句:

for (String k : list) {
    bw.write(k + "\r\n");      
}