62,614
社区成员
发帖
与我相关
我的任务
分享
package Day07;
import java.io.IOException;
import java.io.RandomAccessFile;
public class RandomAccessFileDemo2 {
public static void main(String[] args) throws IOException {
RandomAccessFile raf
=new RandomAccessFile("raf.txt","rw") ;
String str="I read a book in library";
byte[] data=str.getBytes("UTF-8");
raf.seek(0);
byte [] d=new byte[100];
int len=raf.read(d);
String s=new String(d, 0, len,"UTF-8");
System.out.println(s);
raf.seek(0);
int a=raf.skipBytes(0);
System.out.println(raf.getFilePointer());
raf.seek(0);
System.out.println(raf.getFilePointer());
byte [] d1=new byte[100];
int len1=raf.read(d1);
String s1=new String(d1, 1, len1,"UTF-8");
System.out.println(s1);
raf.close();
}
}
public int skipBytes(int n) throws IOException {
long pos;
long len;
long newpos;
if (n <= 0) {
return 0;
}
pos = getFilePointer();
len = length();
newpos = pos + n;
if (newpos > len) {
newpos = len;
}
seek(newpos);
/* return the actual number of bytes skipped */
return (int) (newpos - pos);
}