Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The API docs say the following for readUTF8</p> <blockquote> <p>Reads in a string from this file. The string has been encoded using a modified UTF-8 format.</p> <p>The first two bytes are read, starting from the current file pointer, as if by readUnsignedShort. This value gives the number of following bytes that are in the encoded string, not the length of the resulting string. The following bytes are then interpreted as bytes encoding characters in the modified UTF-8 format and are converted into characters.</p> <p>This method blocks until all the bytes are read, the end of the stream is detected, or an exception is thrown.</p> </blockquote> <p>Is your string formatted in this way?</p> <p>This appears to explain your EOF exceptuon.</p> <p>Your file is a text file so your actual problem is the decoding.</p> <p>The simplest answer I know is:</p> <pre><code>try(BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("jedis.txt"),"UTF-8"))){ String line = null; while( (line = reader.readLine()) != null){ if(line.equals("Obi-wan")){ System.out.println("Yay, I found " + line +"!"); } } }catch(IOException e){ e.printStackTrace(); } </code></pre> <p>Or you can set the current system encoding with the system property <code>file.encoding</code> to UTF-8.</p> <pre><code>java -Dfile.encoding=UTF-8 com.jediacademy.Runner arg1 arg2 ... </code></pre> <p>You may also set it as a system property at runtime with <code>System.setProperty(...)</code> if you only need it for this specific file, but in a case like this I think I would prefer the <code>OutputStreamWriter</code>.</p> <p>By setting the system property you can use <code>FileReader</code> and expect that it will use UTF-8 as the default encoding for your files. In this case for all the files that you read and write.</p> <p>If you intend to detect decoding errors in your file you would be forced to use the <code>InputStreamReader</code> approach and use the constructor that receives an decoder.</p> <p>Somewhat like</p> <pre><code>CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPORT); decoder.onUnmappableCharacter(CodingErrorAction.REPORT); BufeferedReader out = new BufferedReader(new InpuStreamReader(new FileInputStream("jedis.txt),decoder)); </code></pre> <p>You may choose between actions <code>IGNORE | REPLACE | REPORT</code></p> <p><strong>EDIT</strong></p> <p>If you insist in using <code>RandomAccessFile</code>, you would need to know the exact offset of the line that you are intending to read. And not only that, in order to read with <code>readUTF()</code> method, you should have written the file with <code>writeUTF()</code> method. Because this method, as JavaDocs stated above, expects a specific formatting in which the first 2 unsigned bytes represent the length in bytes of the UTF-8 string.</p> <p>As such, if you do:</p> <pre><code>try(RandomAccessFile raf = new RandomAccessFile("jedis.bin", "rw")){ raf.writeUTF("Luke\n"); //2 bytes for length + 5 bytes raf.writeUTF("Obiwan\n"); //2 bytes for length + 7 bytes raf.writeUTF("Yoda\n"); //2 bytes for lenght + 5 bytes }catch(IOException e){ e.printStackTrace(); } </code></pre> <p>You should not have any problems reading back from this file using the method <code>readUTF()</code>, as long as you can determine the offset of the given line that you want to read back.</p> <p>If you'd open the file <code>jedis.bin</code> you would notice <em>it is a binary file</em>, not a text file.</p> <p>Now, I know that <code>"Luke\n"</code> is 5 bytes in UTF-8 and <code>"Obiwan\n"</code> is 7 bytes in UTF-8. And that the <code>writeUTF()</code> method will insert 2 bytes in front of every one of these strings. Therefore, before <code>"Yoda\n"</code> there are (5+2) + (7+2) = 16 bytes.</p> <p>So, I could do something like this to reach the last line:</p> <pre><code>try (RandomAccessFile raf = new RandomAccessFile("jedis.bin", "r")) { raf.seek(16); String val = raf.readUTF(); System.out.println(val); //prints Yoda } catch (IOException e) { e.printStackTrace(); } </code></pre> <p>But this will not work if you wrote the file with a <code>Writer</code> class because writers do not follow the formatting rules of the method <code>writeUFT()</code>.</p> <p>In a case like this, the best would be that your <em>binary</em> file would be formatted in such a way that all strings occupied the same amount of space (number of bytes, not number of characteres, because the number of bytes is variable in UTF-8 depending on the characters in your String), if not all the space is need it you pad it:</p> <p>That way you could easily calculate the offset of a given line because they all would occupy the same amount of space. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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