Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy am I getting an EOF Exception in Java?
    text
    copied!<p>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". All I'm doing is reading the records and adding them to an array list. Here is the code:</p> <pre><code> public boolean buildComparisonArray(String passedPath) throws FileNotFoundException, IOException{ ArrayList&lt;String&gt; comparisonArray = new ArrayList&lt;String&gt;(); 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&lt;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&lt;20-s.length();j++){ raf.readByte(); } s2 = raf.readUTF(); comparisonArray.add(s2); System.out.println("Found in file: "+s2); for(int j=0; j&lt;20-s2.length();j++){ raf.readByte(); } s3 = raf.readUTF(); comparisonArray.add(s3); System.out.println("Found in file: "+s3); for(int j=0; j&lt;20-s2.length();j++){ raf.readByte(); } s4 = raf.readUTF(); comparisonArray.add(s4); System.out.println("Found in file: "+s4); for(int j=0; j&lt;20-s2.length();j++){ raf.readByte(); } s5 = raf.readUTF(); comparisonArray.add(s5); System.out.println("Found in file: "+s5); for(int j=0; j&lt;20-s2.length();j++){ raf.readByte(); } s6 = raf.readUTF(); comparisonArray.add(s6); System.out.println("Found in file: "+s6); for(int j=0; j&lt;20-s2.length();j++){ raf.readByte(); } s7 = raf.readUTF(); comparisonArray.add(s7); System.out.println("Found in file: "+s7); for(int j=0; j&lt;20-s2.length();j++){ raf.readByte(); } s8 = raf.readUTF(); comparisonArray.add(s8); System.out.println("Found in file: "+s8); for(int j=0; j&lt;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; } </code></pre> <p>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. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload