本帖最后由 淡淡柠檬茶 于 2014-6-18 20:20 编辑
import java.io.*;
class RandomF //很重要!!!
{
public static void main(String[] args) throws IOException
{
//readFile();
//注意:可以随机的读和写,流在操作数据的时候只能按照顺序,
writeFile_2();
}
public static void readFile() throws IOException
{
RandomAccessFile w = new RandomAccessFile("1.txt","r");
w.seek(8*0); //调整对象中的指针
byte[] buf = new byte[4];
w.read(buf);
String name = new String(buf);
int age = w.readInt(); //读取一个32位整数
System.out.println("name=..."+name);
System.out.println("age=..."+age);
w.close();
}
public static void writeFile_2() throws IOException
{
RandomAccessFile w1 = new RandomAccessFile("1.txt","r");//想要实现第四个位置是周期
w1.seek(8*3);
w1.write("周期".getBytes());
w1.writeInt(19);
w1.close();
}
}
报错如下
Exception in thread "main" java.io.IOException: 拒绝访问。
at java.io.RandomAccessFile.writeBytes0(Native Method)
at java.io.RandomAccessFile.writeBytes(RandomAccessFile.java:520)
at java.io.RandomAccessFile.write(RandomAccessFile.java:537)
at RandomF.writeFile_2(22.java:28)
at RandomF.main(22.java:9) |
|