-------
android培训
、
java培训
、期待与您交流! ----------
package com.itcast.b21.io.randomAccessFile;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class RandomAccessFileTest
{
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
/**
* 1.写文件
*/
//randomWrite();
/**
* 2.在指定位置写
*/
//randomWrite2();
/**
* 3.在指定位置读
*/
RandomAccessFile raf = new RandomAccessFile("test/raf.txt","r");
raf.seek(10*1);
byte[] buf = new byte[6];
raf.read(buf);
String name = new String(buf);//以代码编辑器平台字符集设置解码
System.out.println(name);
int age = raf.readInt();
System.out.println(age);
raf.close();
}
public static void randomWrite2() throws FileNotFoundException, IOException
{
RandomAccessFile raf = new RandomAccessFile("test/raf.txt","rw");
raf.seek(2*10);//在第三段写
raf.write("周七".getBytes());
raf.writeInt(99);
raf.close();
}
public static void randomWrite() throws FileNotFoundException, IOException
{
RandomAccessFile raf = new RandomAccessFile("test/raf.txt","rw");//必须带上r
System.out.println("王五".getBytes().length);
raf.write("王五".getBytes());
raf.writeInt(97);
raf.write("赵六".getBytes());
raf.writeInt(98);
raf.close();
}
}
-------
android培训
、
java培训
、期待与您交流! ----------