在java中,如何将文件中的一系列小数读到一个数组中,假设文件的格式为:
65949
38729
61122.2
我用DataInputStream类,但是读出的不是我想要的结果,那位大侠能够指点一下,小弟多谢了!

解决方案 »

  1.   

    看到你是说从文件中
    FileInputStream fis = new FileInputStream("c://autoexec.bat");
    InputStreamReader isr = new InputStreamReader(fis);
    BufferedReader br = new BufferedReader(isr);String s;while((s=br.readLine())!=null)
    System.out.prrintln(s);br.close()
      

  2.   

    whd2505() 说的也够详细了额
      

  3.   

    import java.io.*;class StreamTest{
    public static void main(String[] args) throws Exception{
    RandomAccessFile br=new RandomAccessFile("1.txt","rw"); 
    String s; int i=0;
    int j=0;
    while((s=br.readLine())!=null){
    i++;
    }
    float[] fa=new float[i];
    br.seek(0);
    while((s=br.readLine())!=null){
    try{
    fa[j]=Float.parseFloat(s);
    }catch(NumberFormatException ex){
    continue;//如果某一行不符合数字格式,跳过不写进数组
    }
    System.out.println(fa[j]);
    j++;
    }
    br.close();
    }
    }
    看看这是不是你想要的。
    关键是利用RandomAccessFile的seek()方法,不然的话,确定数组可能的最大长度之后再回到文件的开头很困难。
      

  4.   

    FileInputStream fi = new FileInputStream("地址");
    InputStreamReader in = new InputStreamReader(fi);
    BufferedReader bu =new BufferedReader(in);
    下面的操作用bu就可以了