package cn.itcast.randomaccessfile.demo;
import java.io.IOException;
import java.io.ObjectInputStream.GetField;
import java.io.RandomAccessFile;
public class RandomAccessFileDemo {
public static void main(String[] args) throws IOException {
writemethod();
// readmethod();
}
public static void readmethod() throws IOException {
RandomAccessFile raf=new RandomAccessFile("raf.txt", "r");
// raf.seek(1*8);//随机的读取。只要指定指针的位置即可。
byte[] buf = new byte[4];
raf.read(buf);
String name = new String(buf,"gbk");
int age = raf.readInt();
System.out.println("name="+name);
System.out.println("age="+age);
System.out.println("pos:"+raf.getFilePointer());
raf.close();
}
public static void writemethod() throws IOException {
RandomAccessFile raf=new RandomAccessFile("raf.txt", "rw");
raf.write("哈哈".getBytes("gbk"));
raf.writeInt(97);
raf.write("嘻嘻".getBytes("gbk"));
raf.writeInt(99);
raf.close();
}
}
java 随机访问文件RandomAccessFile
猜你喜欢
转载自blog.csdn.net/TDOA1024/article/details/82707267
今日推荐
周排行