JAVA 读文件数据

来源:百度知道 编辑:UC知道 时间:2024/06/01 12:52:44
a.dat文件中有题目涉及的数据如:“10 20 30 61 71 81”共6个数据怎么样把这些数据读到数组中a中如 a[0]=10 a[1]=20.... a[5]=81

public static void main(String[] args) throws IOException {

File file=new File("D:\\a.bat");
FileInputStream io=new FileInputStream(file);
InputStreamReader read=new InputStreamReader(io);
BufferedReader bu=new BufferedReader(read);
String xx=bu.readLine();
String[] values=xx.split(" ");
//values是字符串型数组
//如果要int型的
for(int i=0;i<values.length;i++){
int num=Integer.valueOf(values[i]);
System.out.println(num);
}
}

看一下java/io操作吧