import java.io.*;public class RandomExample {
public static void main(String[] args){
File f=new File("hello.txt");
try{
RandomAccessFile random= new RandomAccessFile(f,"rw");
random.seek(0l);
long m=random.length();
while(m>=0){
m--;
random.seek(m);
int c=random.readByte();
if(c<=255&&c>=0){
System.out.print(c);
}else{
m--;
random.seek(m);
byte cc[]=new byte[2];
random.readFully(cc);
System.out.print(new String(cc));
}
}
long forthEndPosition=0;
int forth=1;
byte n=-1;
while((n=random.readByte())!=-1&&(forth<=4)){
if(n=='\n'){
forthEndPosition=random.getFilePointer();
forth++;
}
}
random.seek(random.length());
long endPosition=random.getFilePointer();
long =endPosition;
int j=1;
while((>=0)&&j<=6){
--;
random.seek();
n=random.readByte();
if(n=='\n'){
endPosition=random.getFilePointer();
j++;
}
}
random.seek(forthEndPosition);
long startPoint=random.getFilePointer();
while(startPoint<endPosition-1){
n=random.readByte();
System.out.print((char)n);
startPoint=random.getFilePointer();
}
random.close();
}catch(IOException e){
System.out.println(e);
}
}
}
public static void main(String[] args){
File f=new File("hello.txt");
try{
RandomAccessFile random= new RandomAccessFile(f,"rw");
random.seek(0l);
long m=random.length();
while(m>=0){
m--;
random.seek(m);
int c=random.readByte();
if(c<=255&&c>=0){
System.out.print(c);
}else{
m--;
random.seek(m);
byte cc[]=new byte[2];
random.readFully(cc);
System.out.print(new String(cc));
}
}
long forthEndPosition=0;
int forth=1;
byte n=-1;
while((n=random.readByte())!=-1&&(forth<=4)){
if(n=='\n'){
forthEndPosition=random.getFilePointer();
forth++;
}
}
random.seek(random.length());
long endPosition=random.getFilePointer();
long =endPosition;
int j=1;
while((>=0)&&j<=6){
--;
random.seek();
n=random.readByte();
if(n=='\n'){
endPosition=random.getFilePointer();
j++;
}
}
random.seek(forthEndPosition);
long startPoint=random.getFilePointer();
while(startPoint<endPosition-1){
n=random.readByte();
System.out.print((char)n);
startPoint=random.getFilePointer();
}
random.close();
}catch(IOException e){
System.out.println(e);
}
}
}
public static void main(String[] args){
File f=new File("hello.txt");
try{
RandomAccessFile random= new RandomAccessFile(f,"rw");
random.seek(0l);
long m=random.length();
while(m>=0){
m--;
random.seek(m);
int c=random.readByte();
if(c<=255&&c>=0){
System.out.print(c);
}else{
m--;
random.seek(m);
byte cc[]=new byte[2];
random.readFully(cc);
System.out.print(new String(cc));
}
}
long forthEndPosition=0;
int forth=1;
byte n=-1;
while((n=random.readByte())!=-1&&(forth<=4)){
if(n=='\n'){
forthEndPosition=random.getFilePointer();
forth++;
}
}
random.seek(random.length());
long endPosition=random.getFilePointer();
long =endPosition;
int j=1;
while((>=0)&&j<=6){
--;
random.seek();
n=random.readByte();
if(n=='\n'){
endPosition=random.getFilePointer();
j++;
}
}
random.seek(forthEndPosition);
long startPoint=random.getFilePointer();
while(startPoint<endPosition-1){
n=random.readByte();
System.out.print((char)n);
startPoint=random.getFilePointer();
}
random.close();
}catch(IOException e){
System.out.println(e);
}
}
}
while (m >= 0) {
m--;
random.seek(m);逻辑有问题,当m=0的时候,自减1,m=-1
seek()这个方法设置读写指针的位置,你设置为-1,所以才会出现异常。
m--;
random.seek(m);
还是有错。不过Negative seek offset的错误就没有了。
m--;
random.seek(m);
还是有错。不过Negative seek offset的错误就没有了
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Unhandled exception type FileNotFoundException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException at RandomExample.main(RandomExample.java:7)
但是我的文件的确是存在的。怎么会FileNotFoundException?
seek()这个方法设置读写指针的位置,你设置为-1,所以才会出现异常。