Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle disk full errors in java

I am working on a Java application. We read lot of data, manipulate it and then write to files in local m/c. If, in any case, the disk is full then how to handle this exception in Java application.

like image 510
ikvenu2000 Avatar asked Sep 28 '11 11:09

ikvenu2000


3 Answers

You can take a look here.
This workaround solves the problem that no exception is thrown when your disk is full.

Basically, it is done by this:

FileOutputStream fos = ...;
fos.write("hello".getBytes());
fos.getFD().sync();
fos.close();

The call to the sync() method will throw a SyncFailedException, when the disk is full.

like image 65
francesco.s Avatar answered Nov 13 '22 21:11

francesco.s


Can you specify more exactly what do you mean when you say how to handle this exception?

The way I see it there are two ways:

  • either you will present that information to user and then the user will be required to clean up some disk space
  • or you will delete some of the unneeded data that you manipulate on your own, from the application, for example the data that has been for the longest time in the system or by some other criteria.
like image 28
Rade_303 Avatar answered Nov 13 '22 21:11

Rade_303


This is a good blog post on the topic: http://weblog.janek.org/Archive/2004/12/20/ExceptionWhenWritingToAFu.html

Also, this bug ticket for Java, explains various strategies: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4338871

like image 2
kevinarpe Avatar answered Nov 13 '22 22:11

kevinarpe