【问题标题】:How to split the strings in a file and read them?如何拆分文件中的字符串并读取它们?
【发布时间】:2013-09-04 07:04:19
【问题描述】:

我有一个包含信息的文件。它看起来像:

    Michael 19 180 Miami
    George 25 176 Washington
    William 43 188 Seattle

我想拆分行和字符串并阅读它们。我希望它看起来像:

    Michael
    19
    180
    Miami
    George
    ...

我使用了这样的代码:

    BufferedReader in = null;
    String read;
    int linenum;
    try{
        in = new BufferedReader(new FileReader("fileeditor.txt")); 
    }
    catch (FileNotFoundException e) {System.out.println("There was a problem: " + e);}
    try{
        for (linenum = 0; linenum<100; linenum++){
            read = in.readLine();
            if(read == null){} 
            else{
                String[] splited = read.split("\\s+");
                System.out.println(splited[linenum]);
           }
       }
    }
    catch (IOException e) {System.out.println("There was a problem: " + e);} 
}

这给了我的是

    Michael
    25
    188

我认为这可能是我的 for 循环的问题,但我在编程方面不是很先进,我将不胜感激。谢谢。

【问题讨论】:

    标签: java split filereader


    【解决方案1】:

    你已经成功了,这很棒。

    读取文件时,Reader 将在到达流末尾时返回 null,这意味着没有其他内容可供读取。您当前的方法意味着您要读取至少 100 行,但不再阅读...如果您增加文件大小,将来这将成为问题...这也有点浪费

    相反,我们应该使用null 值表示文件结尾的事实。..

    当你分割一行时,它将包含许多元素。您正在使用linenum 变量来打印这些。问题是,您已经阅读并拆分了行,linenum 与此任务无关,因为它代表您已经阅读的行数,而不是您刚刚拆分的字符串部分。

    相反,您需要使用内部循环来显示每行的单独拆分元素...

    例如...

    BufferedReader in = null;
    try {
        in = new BufferedReader(new FileReader("fileeditor.txt"));
        String read = null;
        while ((read = in.readLine()) != null) {
            String[] splited = read.split("\\s+");
            for (String part : splited) {
                System.out.println(part);
            }
        }
    } catch (IOException e) {
        System.out.println("There was a problem: " + e);
        e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (Exception e) {
        }
    }
    

    另外,别忘了,如果你打开它,你会发霉地关闭它;)

    您可能还想花更多时间浏览Basic I/O ;)

    【讨论】:

    • 爱你的答案,哥们。总是很好地表达和表达。
    【解决方案2】:
     String[] splited = read.split("\\s+");
      for (int i= 0; i<splited.length; i++){
      System.out.println(splited[i]);
      }
    

    你应该在拆分字符串后循环结果。

    【讨论】:

      【解决方案3】:

      您可以使用StreamTokenizer。它将根据其设置将流拆分为令牌。根据您的问题,我认为您希望将行尾视为标记分隔符。代码如下所示:

      import java.io.BufferedReader;
      import java.io.FileReader;
      import java.io.IOException;
      import java.io.StreamTokenizer;
      
      public class ReaderSample {
      
          public static void main(String[] args) {
              BufferedReader in = null;
              try {
                  in = new BufferedReader(new FileReader("fileeditor.txt"));
                  StreamTokenizer st = new StreamTokenizer(in);
                  st.eolIsSignificant(false);
                  // remove comment handling
                  st.slashSlashComments(false);
                  st.slashStarComments(false);
      
                  while(st.nextToken() != StreamTokenizer.TT_EOF) {
                      if (st.ttype == StreamTokenizer.TT_NUMBER) {
                          // the default is to treat numbers differently than words
                          // also the numbers are doubles
                          System.out.println((int)st.nval);
                      }
                      else {
                          System.out.println(st.sval);
                      }
                  }
              }
              catch(IOException ex) {
                  System.err.println(ex.getMessage());
              }
              finally {
                  if (in != null) {
                      try {
                          in.close();
                      }
                      catch (IOException ex) {
                      }
                  }
              }
          }
      }
      

      根据您需要做什么输入,您可能需要设置不同的选项,文档应该在这里为您提供帮助。

      【讨论】:

      • 重新考虑使用Scanner 似乎更好。 StreamTokenizer已过时。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 2021-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多