什么是Java中更优雅的方式来读取行并基于其中一个字段组合重复的条目

时间:2014-02-27 21:36:29

标签: java readline

以下Java代码从输入文本文件中读取行,并将字段-2中编号最大的条目输出到输出文件。

INPUT:
William J. Clinton 6 Q1124
美国第42任总统6 Q1124
比尔克林顿,6,Q1124 比尔克林顿,14,Q9890
比尔克林顿,5,Q9880
William Jefferson Clinton,6,Q1124

输出:
William J. Clinton,Q1124
第42届美国总统,Q1124
比尔克林顿,Q9890(这个Q-ID被选中,因为第二场是最高的数字“14”) 威廉杰斐逊克林顿,Q1124

是否有比我在以下代码中使用的方法更优雅的方法,可以产生相同的输出?

/*
 * This class uses a method to combine duplicate Wiki keys into one and
 * selecting the Qid of the one with largest link count
 */

public static void main(String[] args) {

    if (args.length < 2) {
        usage();
        System.exit(0);
    }

    //
    try {
        // Create output file
        BufferedWriter out = Files.newBufferedWriter(Paths.get(args[1]),
                StandardCharsets.UTF_8);

        // read input file
        BufferedReader br = Files.newBufferedReader(Paths.get(args[0]),
                StandardCharsets.UTF_8);
        String key = "";
        int links = 0;
        String qID = "";

        String next, line = br.readLine();
        for (boolean first = true, last = (line == null); !last; first = false, line = next) {
            last = ((next = br.readLine()) == null);

            String[] entry = line.split(",");

            if (first) {
                // only assign current values
                key = entry[0];
                links = new Integer(entry[1]);
                qID = entry[2];

            } else if (last) {

                //check if key is a duplicate
                if(key.equals(entry[0])){
                    //use the Qid from the highest links
                    int temp = new Integer(entry[1]);
                    if(links < temp){
                        //write entry to output higher links
                        out.write(key + "," + entry[2] + "\n");
                    }else{
                        //write entry to output using current
                        out.write(key + "," + qID + "\n");
                    }


                }else{
                    //write last entry to output
                    out.write(key + "," + qID + "\n");
                    out.write(entry[0] + "," + entry[2] + "\n");
                }

            } else {

                //check if key is a duplicate
                if(key.equalsIgnoreCase(entry[0])){
                    //use the Qid from the highest links
                    int temp = new Integer(entry[1]);
                    if(links < temp){
                        links = temp;
                        qID = entry[2];
                    }


                }else{
                    //write entry to output
                    out.write(key + "," + qID + "\n");

                    //reassign current values
                    key = entry[0];
                    links = new Integer(entry[1]);
                    qID = entry[2];
                }

            }   

        }

        // Close the output stream
        out.close();

    } catch (Exception e) {// Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }

}

private static void usage() {
    System.err.println("Usage: CombineKeys InputFile OutputFile");
}

1 个答案:

答案 0 :(得分:0)

你的for循环有点复杂。您可能希望坚持使用更传统的方法来构造while循环。

BufferedReader br = new BufferedReader(new FileReader("C:\\testing.txt"));
String sCurrentLine = "";

while ((sCurrentLine = br.readLine()) != null) {
...
}