Suppose I need to create a file and before that I need to check size of available disk space for that operation.
Maybe java.io.File.createNewFile()
will throw a IOException
when there is not enough free space?
What would be the best approach?
Suppose I need to create a file and before that I need to check size of available disk space for that operation.
Maybe java.io.File.createNewFile()
will throw a IOException
when there is not enough free space?
What would be the best approach?
In java 1.6
Check this document
In java 1.7
Check this document
In java 8
Check this document
Please check this Example if you need some help for code
Edit: I got this intresting bug while searching for your problem. Please have a look at this.PrintWriter does not throw IOException for full floppy disk
You can use this to check available space.
http://docs.oracle.com/javase/6/docs/api/java/io/File.html#getFreeSpace%28%29
But deal with excception is not a bad idea, in most case I assume that your free space on the disk will be ok
in simple you can check like this.
long freeSpace = new File("d:/").getFreeSpace(); //Here I am checking my d drive free space.