I have a very strange situation: I use a DataInputStream to read a string from a file in UTF8 . If I wrote the read to the file right before it, everything works, and if I just want to read what has already been written down, it falls with an error.

This code works:

 private void read() { String path = "/Users/pavel/Desktop/test/target_text.txt"; try (DataOutputStream out = new DataOutputStream(new FileOutputStream(path)); DataInputStream in = new DataInputStream(new FileInputStream(path))) { out.writeUTF("hello"); String data = in.readUTF(); System.out.println(data); } catch (IOException e) { e.printStackTrace(); } } 

But this does not work anymore:

 private void read() { String path = "/Users/pavel/Desktop/test/target_text.txt"; try (DataOutputStream out = new DataOutputStream(new FileOutputStream(path)); DataInputStream in = new DataInputStream(new FileInputStream(path))) { //out.writeUTF("hello"); // причем я только что записывал //этим методом и знаю что записан файл по правилу UTF8 String data = in.readUTF(); System.out.println(data); } catch (IOException e) { e.printStackTrace(); } } 

Falls with an error:

 java.io.EOFException at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:340) at java.io.DataInputStream.readUTF(DataInputStream.java:589) at java.io.DataInputStream.readUTF(DataInputStream.java:564) 

Please help me understand what my mistake? How can I just read the data using the readUTF() method?

    1 answer 1

    Creating an object of class FileOutputStream using the constructor:

     public FileOutputStream(String name) 

    involves writing data to the beginning of the file, respectively, after performing this operation, the contents of the file will be cleared, therefore, further, when creating an object of the FileInputStream class:

     new FileInputStream(path) 

    You read an already empty file and rightly get an EOFException when you try to read.

    Solutions to the problem:

    1. Create an object of class FileOutputStream using the constructor:

       public FileOutputStream(String name, boolean append) 

      with flag append == true . In this case, the data will be written to the end of the file, therefore, the original contents of the file will be saved and you can successfully read it using the FileInputStream .

    2. Remove DataOutputStream (this case is suitable if you do not need to write something to the file in this code fragment).

    • >>> FileOutputStream (String name) implies writing data to the beginning of the file, respectively, after performing this operation, the contents of the file will be deleted <<< but when I look at the file with a text editor, the required text is not deleted but does not want to read, maybe are we talking about different things? In the file content is there for sure - Pavel
    • one
      @Pavel, Put a break point on the first line of the read() method and follow the debugger method. After each line, see the contents of the file. - post_zeew
    • And thanks a lot, I understood the code does not work if I leave the DataOutputStream not commented out and do not use it then it just clears the file! Right? - Pavel
    • @Pavel, yes, right. - post_zeew