为什么下面的代码从文件中读不出3和2来?importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.nio.ByteBuffer;importjava.nio.chan...
为什么下面的代码从文件中读不出3和2 来?
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class NewBIO {
为什么下面的代码读不出文件中写入的3和2来?
public static void main(String[] args) throws IOException {
FileOutputStream fout=new FileOutputStream("./a.txt");
ByteBuffer bf=ByteBuffer.allocate(1024);
FileChannel fc=fout.getChannel();
bf.putInt(3);
System.out.println(bf.getInt(0));
bf.putInt(2);
System.out.println(bf.getInt(4));
fc.write(bf);
fout.close();
FileInputStream fin=new FileInputStream("./a.txt");
ByteBuffer bf2=ByteBuffer.allocate(1024);
FileChannel fc2=fin.getChannel();
while(fc2.read(bf2)!=-1){
System.out.println(bf2.getInt(0));
System.out.println(bf2.getInt(4));
}
fin.close();
}
}
展开