我想合并这两个文本文件

驱动程序详细信息文本文件:

AB11; Angela
AB22; Beatrice


旅程文本文件:

AB22,Edinburgh ,6
AB11,Thunderdome,1
AB11,Station,5


我希望我的输出仅是姓名和此人去过的地方。它看起来应该像这样:

Angela
  Thunderdone
  Station

Beatrice
  Edinburgh


这是我的代码。我不确定我在做什么错,但是我没有得到正确的输出。

ArrayList<String>  names = new ArrayList<String>();
TreeSet<String>  destinations = new TreeSet<String>();

public TaxiReader() {

    BufferedReader brName = null;
    BufferedReader brDest = null;

    try {

        // Have the buffered readers start to read the text files
        brName = new BufferedReader(new FileReader("taxi_details.txt"));
        brDest = new BufferedReader(new FileReader("2017_journeys.txt"));

        String line = brName.readLine();
        String lines = brDest.readLine();

        while (line != null && lines != null ){

            // The input lines are split on the basis of certain characters that the text files use to split up the fields within them
            String name [] = line.split(";");
            String destination [] = lines.split(",");

            // Add names and destinations to the different arraylists
            String x = new String(name[1]);
            //names.add(x);

            String y = new String (destination[1]);
            destinations.add(y);


            // add arraylists to treemap
            TreeMap <String, TreeSet<String>> taxiDetails = new TreeMap <String, TreeSet<String>> ();
            taxiDetails.put(x, destinations);

            System.out.println(taxiDetails);

            // Reads the next line of the text files
            line = brName.readLine();
            lines = brDest.readLine();

        }

     // Catch blocks exist here to catch every potential error
    } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
         // Finally block exists to close the files and handle any potential exceptions that can happen as a result
        } finally {
            try {
                if (brName != null)
                    brName.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

}


public static void main (String [] args){
    TaxiReader reader = new TaxiReader();
}

最佳答案

您正在并行读取2个文件,我认为这样做不会太好。尝试一次读取一个文件。

另外,您可能想重新考虑数据结构。

第一个文件将key“ AB11”与value“ Angela”相关。映射比数组列表更好:

Map<String, String> names = new HashMap<String, String>();

String key = line.split(",")[0]; // "AB11"
String value = line.split(",")[1]; // "Angela"
names.put(key, value)
names.get("AB11"); // "Angela"


同样,第二个文件将key“ AB11”与多个values“雷电圆顶”,“站”相关。您也可以为此使用地图:

Map<String, List<String>> destinations = new HashMap<String, List<String>>();

String key = line.split(",")[0]; // "AB11"
String value = line.split(",")[1]; // "Station"

if(map.get(key) == null) {
    List<String> values = new LinkedList<String>();
    values.add(value);
    map.put(key, values);
} else {
    // we already have a destination value stored for this key
    // add a new destination to the list
    List<String> values = map.get(key);
    values.add(value);
}


要获得所需的输出:

// for each entry in the names map
for(Map.Entry<String, String> entry : names.entrySet()) {
    String key = entry.getKey();
    String name = entry.getValue();

    // print the name
    System.out.println(name);

    // use the key to retrieve the list of destinations for this name
    List<String> values = destinations.get(key);
    for(String destination : values) {
        // print each destination with a small indentation
        System.out.println("  " + destination);
    }
}

10-06 16:15