JAVA IO - RandomAccessFile

原创
2014/01/08 17:49
阅读数 112

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();
    	}
    }
}



展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部