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()方法,不然的话,确定数组可能的最大长度之后再回到文件的开头很困难。
利用RandomAssessFile来把文件内的东西转化为数组
最新推荐文章于 2022-03-12 18:35:28 发布