建议阅读文本文件和循环

时间:2016-02-28 12:24:19

标签: java file loops readfile

希望有人可以帮助一个初学的Java enthousiast。 我有一个看起来像这样的文本文件:

"AB",101,"ABC","EFG"
"AB",102,"ABC","HIJ"
"AB",103,"ABC","HIJ"
"AB",104,"ABC","XYZ"
"AB",105,"ABC","XYZ"

我已使用以下代码阅读该文件:

List<String> list = null;
        try {
            flights = Files.readAllLines(Paths.get(pathToFile), StandardCharsets.UTF_8);
        } catch (IOException e) {
            System.out.println("No/wrong file");
        }

我知道有一个列表,其中每一行都是一个新元素。我想要做的是遍历此列表并检查每个元素的第四个值(&#34; EFG&#34;,&#34; HIJ&#34;等)并删除重复项。

如果有人能引导我朝着正确的方向前进,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

HashSet<String> alreadyHandled = new HasSet<String>();
for(String line:flights){
    String[] parts = line.split(",");
    String lastPart = parts[parts.length-1]
    if(!alreadyHandled.contains(lastPart)){
        alreadyHandled.add(lastPart);

        //Here you have your code what you want to do with your line
    }
}