【问题标题】:Read from text file with a condition从带有条件的文本文件中读取
【发布时间】:2015-07-29 04:02:51
【问题描述】:

我正在读取一个文本文件,条件是要忽略以 * 开头的单词。

example:
abc 1234 *text to be ignored

所以在这个例子中,我会在从文本文件读取时忽略“要忽略的文本”,并且只会将 abc 和 1234 存储在字符串数组中。

为此,我编写了以下代码。如何实现忽略以 * 开头的单词的条件?

public static void read(String filename) {
        BufferedReader reader = null;

        try {
            String line;
            reader = new BufferedReader (new FileReader(filename));
            while ((line = reader.readLine()) != null) {
                String[] functionName = line.split("\\s+");         
                            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (reader != null)
                try {
                    reader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
    }

【问题讨论】:

  • 那么,如果您看到*,您想从这里忽略所有内容吗?剩余的尾随空间呢?另外,您使用的是什么 Java 版本?

标签: java arrays string file bufferedreader


【解决方案1】:

您不知道您使用的是什么版本的 Java,所以我将假设 Java 8...

注意:代码未经测试,但应该可以进行一些调整。

private static final Pattern SPACES = Pattern.compile("\\s+");
private static final Pattern STAR_TO_END = Pattern.compile("\\s*\\*.*");
public static String[] read(final String filename)
{
    final Path path = Paths.get(filename);

    try (
        // UTF-8 by default; excellent
        final Stream<String> lines = Files.line(path);
    ) {
        return lines.map(line -> STAR_TO_END.matcher(line).replaceFirst(""))
            .flatMap(SPACES::splitAsStream)
            .collect(Collectors.toArray(String[]::new));
    }
}

【讨论】:

    【解决方案2】:

    如果您不想遍历您的单词以检查它是否以 * 开头,您还可以在使用 split 之前从其中删除所有带有星号的单词。

    String str = "abc 1234 *text to be ignored";
    System.out.println(Arrays.toString(str.replaceAll("\\*[^\\s]+\\s*", "").split("\\s+")));
    // [abc, 1234, to, be, ignored]
    str = "*abc *1234 *text to be *ignored";
    System.out.println(Arrays.toString(str.replaceAll("\\*[^\\s]+\\s*", "").split("\\s+")));
    // [to, be]
    

    正则表达式分解

    \\* - Literal match of asterisk
    [^\\s]+ - Match anything but a space
    \\s* - Capture any or no spaces at end of word
    

    【讨论】:

      【解决方案3】:

      你可以试试indexOf()substring()一样

       while ((line = reader.readLine()) != null) {
          if(line.indexOf("*")>-1)
          line=line.substring(0,line.indexOf("*"));
          String[] functionName = line.split("\\s+");  
       }
      

      上面的indexOf("*") 会给你* 的索引,那么你可以通过substring(beginIndex,endIndex) 找到endIndex 作为* 的索引的子字符串。 p>

      【讨论】:

      • 我认为,提供的所有答案中最好的答案
      • 如果没有没有星怎么办?
      • @fge OP dint 提到但万一只是把if(line.indexOf("*")&gt;-1) 我已经更新了我的答案
      【解决方案4】:

      startWith(String literal) 如果您的 String 以给定的字符串文字开头,则返回 true。

      例如:

      "1234".startsWith("12"); 返回true

      所以你应该阅读所有单词并检查它是否开始甚至包含*,如果是,则忽略整个单词。

      示例:

      if(! word.startsWith("*")) {
      // add to what ever you want
      }
      

      if(! word.contains("*")) {
      // add to what ever you want
      }
      

      【讨论】:

      • 不,.startsWith() 不将正则表达式作为参数,而是字符串文字。
      • 已编辑答案,谢谢告知,不知道。
      【解决方案5】:

      您可以在 while 循环中执行类似的操作 -

      while ((line = reader.readLine()) != null) {
         String[] functionName = line.split("\\s+");         
         String newLine = "";
      
         for(String strg : functionName){
      
            if(strg.startsWith("*")){
               break;
            }else{
               newLine = strg + newLine;
            }
      
         }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-13
        • 2016-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多