【问题标题】:Ensuring that empty lines in text file are not added to list?确保文本文件中的空行不添加到列表中?
【发布时间】:2016-08-20 09:56:05
【问题描述】:

我有一个Java method(如下),它检查text filestring 值,然后添加到list

 public List<String> iterateTextFile(String filePath) {

        List<String> lines = new ArrayList<String>();
        try {
            lines = Files.readLines(new File(filePath), Charset.forName("utf-8"));
        }catch(IOException e){
            e.printStackTrace();
        }
        return lines;
    }

但是,此方法也会在 ArrayList 中添加 空白行,例如:""。我不要这个?

如何添加验证以使这种情况不会发生?

【问题讨论】:

    标签: java string list file methods


    【解决方案1】:

    你也可以试试

    List<String> lines = new LinkedList<>();
    int lineCounter = 0;
    Scanner scanner = new Scanner(new File(filePath));
    while(scanner.hasNext()){
        String currentLine = scanner.nextLine();
        lineCounter++;
        if(currentLine!=null && currentLine.length()==0){
            System.out.println("Skipping empty line number:" + lineCounter);
        }else{
            lines.add(currentLine);
        }
    
    }
    

    看,使用计数器变量来指示这是否正常工作。好吧,这段代码显然带有FileNotFoundException 需要处理。

    【讨论】:

      【解决方案2】:

      看看这个答案:只需修改“LineProcessor”以过滤掉空行!

      link

      【讨论】:

        【解决方案3】:

        过滤它们:

        public List<String> iterateTextFile(String filePath) {
        
                List<String> lines = new ArrayList<String>();
                try {
                    lines = Files.readAllLines(Paths.get(filePath)).stream().filter(str -> !str.trim().isEmpty()).collect(Collectors.toList());
                }catch(IOException e){
                    e.printStackTrace();
                }
                return lines;
            }
        

        对于 pre-stream java 版本,你可以这样做(你从最后一个位置到 0,所以你没有重新填充):

        public List<String> iterateTextFile(String filePath) {
        
                List<String> lines = new ArrayList<String>();
                try {
                    lines = Files.readLines(new File(filePath), Charset.forName("utf-8"));
                    eliminateEmptyStrings(list);
                }catch(IOException e){
                    e.printStackTrace();
                }
                return lines;
            }
        
            private void eliminateEmptyStrings(List<String> strings) {
            for (int i = strings.size() - 1; i >= 0; i--) {
               if (strings.get(i).trim().isEmpty()) strings.remove(i);
            }
        }
        

        注意:添加断言以检查空值,否则您可能会遇到一些 NPE 或其他不受控制的异常。

        【讨论】:

          【解决方案4】:

          在返回之前删除空元素:-

          for(int i =0; i < lines.size(); i++) {
            if(list.get(i).equals("")) {
              list.remove(i);
            }
          }
          return list;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-01-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多