Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOException handling during file write in java

Tags:

java

file

io

My app writes loads of bytes constantly to a file. If a block of my code is writing out bytes and gets an IOException if the connection to the drive or network is lost how am I to know how many bytes it has written?

For eg I'm writing 2500 bytes at a time and if an IOException occurs does it mean it wrote nothing to the file or it would have written partial bytes to the file? If partial bytes are written to the file how am I to know how much is written

FYI I'm using

DataOutputStream

to write out files. I flush only when my app closes.

like image 296
Vig Avatar asked Jul 26 '12 14:07

Vig


People also ask

How do you handle IOException in Java?

You should put the code for that in a catch or finally clause for the IOException , even if you would like the exception to propagate upwards. If you use a catch clause and you want to propagate it, you will have to rethrow it.

What is IOException in Java example?

Java IOExceptions are Input/Output exceptions (I/O), and they occur whenever an input or output operation is failed or interpreted. For example, if you are trying to read in a file that does not exist, Java would throw an I/O exception.

What handles IOException?

IOException is the base class for exceptions thrown while accessing information using streams, files and directories.

Why do we throw IOException in Java?

It may occur due to the file deleted or viruses in the file. Sometimes BufferedReader takes data from a network stream where the reading system can fail at any time. So this type of error can occur in input operation when a BufferedReader is used. This is why a buffered reader throws IOException.


2 Answers

You can't, IOException can happen at any time. If you think about it, IOException can be caused by software or hardware failure at any time, before the first byte was written, after all were written but something happened to the connection - or in the middle if the data chunk was pretty big.

After reconnecting you must check the file size or use some other technique to keep files in consistent state and recover. The simplest safety net: read file size before writing data and store it reliably, write the data and read file size after writing. If error occurrs and current file size is not equal to expected file size (last saved), truncate extra bytes and retry.

Read about transaction logs in relational databases to see how big and reliable software handles such cases.

like image 72
Tomasz Nurkiewicz Avatar answered Sep 27 '22 19:09

Tomasz Nurkiewicz


It depends on how the write fails, but the InterruptedIOException may be what you're looking for:

http://docs.oracle.com/javase/1.4.2/docs/api/java/io/InterruptedIOException.html

It contains a field for "bytesTransferred": "Reports how many bytes had been transferred as part of the I/O operation before it was interrupted."

like image 39
xthexder Avatar answered Sep 27 '22 18:09

xthexder