import java.io.*;
import java.util.*;
public class Test
{
public static void main(String[] args) throws Exception
{
//使用Scanner类的nextInt方法
File file = new File("a.txt");
Scanner scan = new Scanner(new FileInputStream(file));
int in = scan.nextInt();
System.out.println(in);
int count = 0;
List<Integer> list = new ArrayList<Integer>(); //用来保存从流中读取出来的数据
while(scan.hasNextInt())
{
//System.out.println("+++++++++");
//Thread.sleep(500);
count++; //由于不能确定文件后面到底有多少个int型数据,所以只好先确定int型数据的个数,作为数组的长度。
in = scan.nextInt();
list.add(in); //先将读取出来的数据保存在list集合中,再从该集合里取出来放入数组里
}
int[] a = new int[count];
for (int i = 0; i < a.length; i++)
{
a[i] = list.get(i);
}
for (int i = 0; i < a.length; i++)
{
System.out.print(a[i] + " ");
}
/*如果源文件中的数据是用DataOutputStream写进去的,可用如下方式读取出来
DataInputStream dis = new DataInputStream(new FileInputStream(file));
int b = dis.readInt();
System.out.println(b);*/
}
}
|