【问题标题】:How to read from inputFileStream and split each line如何从输入文件流中读取并拆分每一行
【发布时间】:2015-03-07 20:20:18
【问题描述】:

我必须从看起来像 mark;1001;3;4 的输入文件 txtfile 中读取每个变量之间有一个 ';'。如果它在单独的行中,我知道如何阅读它,但如果它在同一行中,我无法阅读它。

我是这样开始的:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.nio.Buffer;

public class Try {
    public static void main(String[] args) {
        String Name;
        int ID;
        Double quiz1 , quiz2;

        try {
            FileInputStream fileIN = new FileInputStream("input.txt");
            InputStreamReader inputST =new InputStreamReader(fileIN);
            BufferedReader  bufferRe = new BufferedReader(inputST);

            String line;

            while ((line = bufferRe.readLine()) != null) {
                // I tried many things, but nothing worked for me.
                // How could I use split here?
            }
        } catch (IOException e) {
            System.out.println("input is not found ");
        }
    }
}

【问题讨论】:

  • 您是否尝试将行标记;1001;3;4 拆分为标记 1001 3 4?
  • line.split(";") 就是您所需要的。使用时遇到了什么问题。
  • 使用string[] currentLine = line.split(";");分割每一行
  • 希望您不介意我在while 中更改了您的评论。我希望我做出了正确的更改,因为您的评论很难理解。

标签: java file inputstream string-split


【解决方案1】:

使用拆分是要走的路...

while ( ( line = bufferRe.readLine())!= null) {
    for (String splitVal : line.split(";") {
         //Do whatever you need to with the splitVal value.
         //In you example it iterate 4 times with the values mark 1001 3 4
    }
}

【讨论】:

    【解决方案2】:

    最简单的解决方案是使用 Scanner; 作为其分隔符,也适用于您希望跨换行符的情况:

    Scanner s = new Scanner(bufferRe);
    s.useDelimiter(";");
    while (s.hasNext()) {
        System.out.println(s.next());
    }
    -->
     mark
    1001
    3
    4
    

    这也允许您使用扫描仪方法来例如。轻松解析整数。

    【讨论】:

      【解决方案3】:

      只需在循环中使用split 方法来获取数组中的所有数据。

      String[] splited = line.split(";");
      

      【讨论】:

        【解决方案4】:
        while ((line = bufferRe.readLine()) != null) {
            for (String retval : line.split(";", 2)) {
                System.out.println(retval);
            }
        }
        

        输出:

        mark
        1001;3;4
        

        【讨论】:

          【解决方案5】:

          还有一种使用 StreamTokenizer 的方法

          try {
              FileInputStream fis = new FileInputStream("input.txt");
              Reader r = new BufferedReader(new InputStreamReader(fis));
              StreamTokenizer st = new StreamTokenizer(r);
          
              List<String> words = new ArrayList<>();
              List<Integer> numbers = new ArrayList<>();
          
               // print the stream tokens
               boolean eof = false;
               do {
                  int token = st.nextToken();
                  switch (token) {
                     case StreamTokenizer.TT_EOF:
                        System.out.println("End of File encountered.");
                        eof = true;
                        break;
                     case StreamTokenizer.TT_EOL:
                        System.out.println("End of Line encountered.");
                        break;
                     case StreamTokenizer.TT_WORD:
                        words.add(st.sval);
                        break;
                     case StreamTokenizer.TT_NUMBER:
                        numbers.add((int)st.nval);
                        break;
                     default:
                        System.out.println((char) token + " encountered.");
                        if (token == '!') {
                           eof = true;
                        }
                  }
               } while (!eof);
          
          } catch (IOException e) {
              e.printStackTrace();
              System.out.println("input is not found ");
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-09-27
            • 2015-11-09
            • 2023-04-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多