java如何读取文本文件中的整数数据,数据放在一行是用空格隔开的?
1个回答
展开全部
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);
}
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
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询