我编写了以下方法来读取文件,搜索某个字符“$”,并返回一个指示它是否存在的布尔值。出于某种原因,我一直在第224行得到一个EOFException,它只是说“s = raf.readUTF”。我正在做的就是读取记录并将它们添加到数组列表中。这是代码:
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;
}
我想要做的就是通过一行的随机访问文件读取并检查是否存在“$”符号。
答案 0 :(得分:1)
读完s2后,你忘记将s2改为s3,s2改为s4,依此类推你的for-instruction
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();
}