如果一个文件是这样
AS BD CA 1.1
DS FD EA 3.4
(下略)
每行中间是tab分开的; 怎么样一个一个读出每个String和double ?
C++里面直接用《就可以了,java我只找到一个readline()
有什么简单的method,比如readDouble, readInt, readString这类的么?
T*********e 发帖数: 9208
2
Scanner
public static void main(String[] args) throws IOException {
Scanner s = null;
double sum = 0;
try {
s = new Scanner(
new BufferedReader(new FileReader("usnumbers.txt")));
s.useLocale(Locale.US);
while (s.hasNext()) {
if (s.hasNextDouble()) {
sum += s.nextDouble();
} else {
s.next();
}
}
【在 f*******y 的大作中提到】 : 如果一个文件是这样 : AS BD CA 1.1 : DS FD EA 3.4 : (下略) : 每行中间是tab分开的; 怎么样一个一个读出每个String和double ? : C++里面直接用《就可以了,java我只找到一个readline() : 有什么简单的method,比如readDouble, readInt, readString这类的么?
m******t 发帖数: 2416
3
LZ could also try read one line a time (with readLine()), and use String.
split to split the line into parts.