您的位置:首页 > 编程语言 > Java开发

JAVA IO - RandomAccessFile

2014-01-08 00:00 369 查看

RandomAccessFile

The RandomAccessFile class in the Java IO API allows you to move around a file and read from it or write to it as you please.

Here is a thing the JavaDoc forgets to mention: The read() method increments the file pointer to point to the next byte in the file after the byte just read! This means that you can continue to call read() without having to manually move the file pointer.

import java.io.RandomAccessFile;

public class RandomAccessFileTest {
public static void main(String args[]){
try{
RandomAccessFile file = new RandomAccessFile(
"c:\\work\\hello\\helloworld.txt", "rw");
file.writeBytes("hello world!");
file.writeChar('A');
file.writeInt(1);
file.writeBoolean(true);
file.writeFloat(1.0f);
file.writeDouble(1.0);
file.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  RandomAccessFile