IO读取一个文本(字节、字符)中从最后一个开始读到第一个
package IO_class.work;
import org.junit.Test;
import java.io.*;
public class Demo02 {
//读取一个文本中从最后一个开始读到第一个
//读取字节
@Test
public void method() throws Exception {
//
RandomAccessFile rf = new RandomAccessFile("io0.txt","r");
//用for循环字节长度减一
for (long i = rf.length()-1; i >=0 ; i--) {
Thread.sleep(1000);
//偏移量从最后向前移动
rf.seek(i);
int read =rf.read();
System.out.println((char) read);
//获取偏移量值
long fp = rf.getFilePointer();
System.out.println("偏移量:"+fp);
}
rf.close();
}
//读取字符
@Test
public void method1() throws Exception {
RandomAccessFile rf = new RandomAccessFile("io3.txt","r");
byte[] b = new byte[3];
int read;
//RandomAccessFile rd = new RandomAccessFile("","rw");
//利用for循环每一次减三个字节
for (long i = rf.length()-3; i >=0 ; i-=3) {
Thread.sleep(100);
rf.seek(i);
read =rf.read(b);
System.out.println(new String(b,0,read));
long fp = rf.getFilePointer();
System.out.println("偏移量:"+fp);
}
rf.close();
}
}