簡體   English   中英

為什么在Java中收到EOF異常?

[英]Why am I getting an EOF Exception in Java?

我編寫了以下方法來讀取文件,搜索某個字符“ $”,並返回一個布爾值,指示是否存在該布爾值。 出於某種原因,我在第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;
}

我要做的就是讀取一行隨機訪問文件,並檢查是否存在“ $”符號。

閱讀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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM