public class readfile 
{ public static void main(String[] args) 
{
try
{
File of=new File("./test.txt");
FileInputStream OFIS=new FileInputStream(of);
int nBuffer=-1;
while(-1!=(nBuffer=OFIS.read()))
{

System.out.print((char)nBuffer);

}
}
catch(IOException e)
{
System.out.println(e);
System.exit(1);
}
}
}
test.txt中为一系列的数字,如:32 30 50 
                              20 30 50 
想把这些值赋给一个数组a[][]用于计算中,不知道怎么弄?谁弄帮帮我吗?

解决方案 »

  1.   

    给个参考的例子:但是必须是读入的文件保持楼主所说的格式,比如说一律一个三个或者几个,如果不定长,就考虑用Vector
    try {
                int[][] a = new int[MAX][3];
                FileReader fs = new FileReader("E:\\Eclipse\\Temp\\src\\text.txt");
                BufferedReader br = new BufferedReader(fs);
                String record = new String();
                int i = 0;
                while((record = br.readLine()) != null)
                {
                    StringTokenizer st = new StringTokenizer(record);
                    int j = 0;
                    while(st.hasMoreTokens())
                    {
                        a[i][j] = Integer.parseInt(st.nextToken());
                        j++;
                    }
                    i++;            
                }
                           
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
      

  2.   

    我认为最后使用DataInputStream和DataOutputStream包装类
      

  3.   

    我认为 String[]=txt.split("分隔符")更合适