public static void ReadWorkingList() { DataInputStream inputStream = new DataInputStream(new FileInputStream("E:\\Working")); while (inputStream.available() > 0) { System.out.println(inputStream.readUTF()); } } public static void WorkingDelete() throws Exception { BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); DataInputStream in = new DataInputStream(new FileInputStream("E:\\Working")); DataOutputStream outWrite = new DataOutputStream(new FileOutputStream("E:\\tmp")); while (in.available()>0) { System.out.println(in.readUTF()); } System.out.println("Введите имя человека, которого нужно удалить"); String deleteName = read.readLine(); while (in.available()>0) { String tmp = in.readUTF(); if(tmp.equals(deleteName))//сравнение введённого имени со строками в файле, если есть такое, то след.итерация. { continue; } outWrite.writeUTF(tmp); } //запись из файла TMP в файл DataInputStream outTMPinWorkList = new DataInputStream(new FileInputStream("E:\\tmp")); DataOutputStream WriteList = new DataOutputStream(new FileOutputStream("E:\\Working")); while (outTMPinWorkList.available()>0) { WriteList.writeChars(outTMPinWorkList.readUTF()); // Запись из одного файла в другой } outTMPinWorkList.close(); WriteList.close(); read.close(); in.close(); outWrite.close(); } 

Tell me why EOFEexception crashes? Why he does not have time to count? And why doesn't WorkingDelete write to the tmp file? Help me please

  • 2
    try closing the writing stream before opening the file with a new one for reading ( outWrite.close() ). - zRrr 6:49 pm
  • And because of what such EOFExcpetion can take off? For example, in this code, "DataInputStream inputStream = new DataInputStream (new FileInputStream (" E: \\ Working ")); System.out.println (inputStream.readUTF ());" The file is not empty, there is data - Anton
  • what data? Can you show the string and hex header? at the beginning of the file, is there no EOF byte? - Evgeny Lebedev
  • I read a text file, the file is a regular line with Cyrillic, I want to output it to the console, and the crash crashes. And once in a while he crashes. If, for example, the file is changed from "Working" to, for example, "Working1" - everything is fine at first, and then this murkiness begins again - Anton
  • You did not write in which line it crashes, but I will try to guess the insides of the sacrificial documentation. The DataInputStream methods throw an EOF if the stream ended earlier than they could read enough bytes to complete, which can be if the file is not written to disk, for example. But you still have another error: writeChars not written in the format that expects readUTF , after the first start the program writes to E:\Working content that it cannot read. - zRrr

1 answer 1

The java.io.EOFException error is popping up because of readUTF, this method is not intended to read plain text.

There, the first two bytes are the length of the text, then the text goes.

Try to write the text like this:

 try (DataOutputStream outputStream = new DataOutputStream(new FileOutputStream("D:\\1.txt"))) { outputStream.writeUTF("test"); } catch (IOException e) { e.printStackTrace(); } 

00 04 74 65 73 74 (this is in hexadecimal) is written to the file.

And from this file you can already read the text using readUTF.

And when you read a plain text file, readUTF interprets the first two characters (bytes) as length, gets a large number and naturally cannot read.

To read a plain text file, use BufferedReader.

  • Greatly grateful to you, I fought for several hours over this, and did not notice where I used crutches :) - Anton