使用Scanner按数据类型从文件读取
import java.io.FileReader; import java.io.BufferedReader; /* 来自 *nowjava*/ import java.io.IOException; import java.util.Scanner; import java.util.Locale; public class ScanSum { 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()) {//来 自 N o w J a v a . c o m sum += s.nextDouble(); } else { s.next(); } } } finally { s.close(); } System.out.println(sum); } }