[英]Why am I getting an EOF Exception in Java?
I wrote the following method to read a file, search for a certain character "$", and return a boolean indicating whether it is present or not. 我编写了以下方法来读取文件,搜索某个字符“ $”,并返回一个布尔值,指示是否存在该布尔值。 For some reason, I keep getting a EOFException at line 224, where it simply says "s = raf.readUTF".
出于某种原因,我在第224行不断收到EOFException,它只说“ s = raf.readUTF”。 All I'm doing is reading the records and adding them to an array list.
我要做的就是读取记录并将它们添加到数组列表中。 Here is the code:
这是代码:
public boolean buildComparisonArray(String passedPath) throws FileNotFoundException, IOException{
ArrayList<String> comparisonArray = new ArrayList<String>();
boolean unwantPresent = false;
File file = new File(passedPath);
RandomAccessFile raf = new RandomAccessFile(file, "r");
raf.seek(0);
long num = 0;
long fileSize = 0;
int record = 0;
fileSize = raf.length();
record = 160;
num = fileSize/record;
String s = "", s2 = "", s3="",s4="",s5="",s6="",s7="",s8="";
long currentPositionOfFilePointer;
for(int i=0; i<num; i++){
currentPositionOfFilePointer = raf.getFilePointer();
System.out.println("Current: "+currentPositionOfFilePointer);
s = raf.readUTF();
comparisonArray.add(s);
System.out.println("Found in file: "+s);
for(int j=0; j<20-s.length();j++){
raf.readByte();
}
s2 = raf.readUTF();
comparisonArray.add(s2);
System.out.println("Found in file: "+s2);
for(int j=0; j<20-s2.length();j++){
raf.readByte();
}
s3 = raf.readUTF();
comparisonArray.add(s3);
System.out.println("Found in file: "+s3);
for(int j=0; j<20-s2.length();j++){
raf.readByte();
}
s4 = raf.readUTF();
comparisonArray.add(s4);
System.out.println("Found in file: "+s4);
for(int j=0; j<20-s2.length();j++){
raf.readByte();
}
s5 = raf.readUTF();
comparisonArray.add(s5);
System.out.println("Found in file: "+s5);
for(int j=0; j<20-s2.length();j++){
raf.readByte();
}
s6 = raf.readUTF();
comparisonArray.add(s6);
System.out.println("Found in file: "+s6);
for(int j=0; j<20-s2.length();j++){
raf.readByte();
}
s7 = raf.readUTF();
comparisonArray.add(s7);
System.out.println("Found in file: "+s7);
for(int j=0; j<20-s2.length();j++){
raf.readByte();
}
s8 = raf.readUTF();
comparisonArray.add(s8);
System.out.println("Found in file: "+s8);
for(int j=0; j<20-s2.length();j++){
raf.readByte();
}
}
raf.close();
for(String j: comparisonArray){
if(j.contains("$")){
unwantPresent = true;
}else if(!j.contains("$")){
unwantPresent = false;
}
}
return unwantPresent;
}
All i'm trying to do is read through a random access file of one line and check whether or not the "$" symbol is present. 我要做的就是读取一行随机访问文件,并检查是否存在“ $”符号。
After you read s2 u forgot to change s2 to s3, s2 to s4 and so on in your for-instruction 阅读s2之后,您忘了将s2更改为s3,将s2更改为s4,以此类推。
s2 = raf.readUTF();
comparisonArray.add(s2);
System.out.println("Found in file: "+s2);
for(int j=0; j<20-s2.length();j++){
raf.readByte();
}
s3 = raf.readUTF();
comparisonArray.add(s3);
System.out.println("Found in file: "+s3);
for(int j=0; j<20-s2.length();j++){
raf.readByte();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.