java如何读取文本文件中的整数数据,数据放在一行是用空格隔开的?

如题所述

第1个回答  2009-03-13
The following example illustrates how the String.split method can be used to break up a string into its basic tokens:

String[] result = "this is a line read from txt file".split("\\s");
for (int x=0; x<result.length; x++)
System.out.println(result[x]);

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
String[] result = "123 321 -1 555".split("\\s");
for (int x=0; x<result.length; x++){
int value= -999999; //default
try{
value= Integer.parseInt(result[x]);
}catch (Exception e){}
System.out.println(value);
}

参考资料:http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html

本回答被提问者采纳
相似回答