0

我编写了以下方法来读取文件,搜索某个字符“$”,并返回一个布尔值,指示它是否存在。出于某种原因,我在第 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;
}

我要做的就是通过一行的随机访问文件读取并检查是否存在“$”符号。

4

1 回答 1

1

在您阅读 s2 后,您忘记在您的 for 指令中将 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();
}
于 2013-03-03T17:29:15.213 回答