【发布时间】: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