首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > Access >

在资料指定位置读取和写入内容RandomAccessFile

2012-10-08 
在文件指定位置读取和写入内容RandomAccessFileRandomAccessFile是属于随机读取类,是可以对文件本身的内容

在文件指定位置读取和写入内容RandomAccessFile

RandomAccessFile是属于随机读取类,是可以对文件本身的内容直接随机进行操作的,可以在文件的指定位置
的读取和写入内容,这在很多时候都是很方便的。

import java.io.IOException;import java.io.RandomAccessFile;/** * RandomAccessFile是属于随机读取类,是可以对文件本身的内容直接随机进行操作的,就是说可以指定位置 * 的读取和写入内容 * @author andy * */public class RandomAccessFileTest {public static void main(String args[]) throws IOException {write();read();}public static void write() throws IOException {//以读写的方式来访问该文件RandomAccessFile raf = new RandomAccessFile("D:\\test.txt", "rw");raf.writeBytes("Hello World!");raf.writeBoolean(true);raf.writeInt(30);raf.writeDouble(3.56);raf.close();}public static void read() throws IOException {RandomAccessFile raf = new RandomAccessFile("D:\\test.txt", "r");raf.seek(12);//设置指针的位置boolean booleanValue = raf.readBoolean();int intValue = raf.readInt();double doubleValue = raf.readDouble();raf.seek(0);//设置指针的位置为文件的开始部分byte[] bytes = new byte[12];for (int i=0; i<bytes.length; i++)bytes[i] = raf.readByte();//每次读一个字节,并把它赋值给字节bytes[i]String stringValue = new String(bytes);raf.skipBytes(1);//指针跳过一个字节int intValue2 = raf.readInt();raf.close();System.out.println(booleanValue);System.out.println(intValue);System.out.println(doubleValue);System.out.println(stringValue);System.out.println(intValue2);}}

热点排行