| 发表于:2007-05-23 23:27:165楼 得分:6 |
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()方法,不然的话,确定数组可能的最大长度之后再回到文件的开头很困难。 | | |
|