如何从inputFileStream中读取并拆分每一行

时间:2015-03-07 20:20:18

标签: java file inputstream string-split

我必须从输入文件 txtfile 中读取,看起来像mark;1001;3;4每个变量之间都有';'。我知道怎么读它,如果它是在不同的行,但我不能读它,如果它在同一行。

这就是我的开始:

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 ");
        }
    }
}

5 个答案:

答案 0 :(得分:2)

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

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
    }
}

答案 1 :(得分:1)

最简单的解决方案,当您希望事情跨越换行符时也能正常工作,就是使用Scanner ;作为分隔符:

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

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

while ((line = bufferRe.readLine()) != null) {
    for (String retval : line.split(";", 2)) {
        System.out.println(retval);
    }
}

输出:

mark
1001;3;4

答案 4 :(得分:0)

还有一个使用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 ");
}