代码运行出来 文件里是乱码而且 Arrays.toString()全是0
package RandomAccessFile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
public class Randomaccessfile {
public static void createfile(String filename){
File file=new File(filename);
if(!file.exists()){
file.mkdir();
}else{
System.out.println("文件已存在");
}
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Randomaccessfile.createfile("demo\\r.dat");
RandomAccessFile r=new RandomAccessFile("r.dat","rw");
System.out.println(r.getFilePointer());
for(int i=1;i<6;i++){
r.writeDouble(i*1.422);//一个double类型占8个字节
}
System.out.println(r.getFilePointer());
//把指针指到第四个数据后面
r.seek(32);//r.seek(4*8);
r.writeDouble(7*1.2212);//替换第五个数据
System.out.println(r.getFilePointer());
r.close();
RandomAccessFile ra=new RandomAccessFile("r.dat","r");
for(int i=1;i<6;i++){
System.out.println("Value "+i+":"+ra.readDouble());;//一个double类型占8个字节
}
byte[] bytes=new byte[(int)ra.length()];
ra.read(bytes);
System.out.print(Arrays.toString(bytes));
}
}