In Java SE 6.0, the
File
class has been enhanced to include new methods that provide information about disk usage:getTotalSpace()
getFreeSpace()
getUsableSpace()
getTotalSpace()
- The
getTotalSpace
method provides information on a particular File's partition size in bytes. getFreeSpace()
- The
getFreeSpace
method provides information about the available space still unused in a particular File partition in bytes. getUsableSpace()
- Similar to
getFreeSpace
, thegetUsableSpace
method further checks for OS restrictions and available write permissions associated with the File.
This code is an example of how to use the
getTotalSpace
, getFreeSpace
, and getUsableSpace
methods.import java.io.*; public class SampleFileMethod { public static void main(String args[]) { if(args.length>0) { File f=new File(args[0]); System.out.println(" Total size of " + f + " is=:"+ f.getTotalSpace()); System.out.println(" Available space of " + f + " is=:"+ f.getFreeSpace()); System.out.println(" Usable space of " + f + " is=:"+ f.getUsableSpace()); } else { System.out.println(" Enter a Filename"); } } }
Additional methods used to specify restrictions or permissions in reading, writing, or executing a
File
object aresetWritable()
setReadable()
setExecutable():
setWritable()
- The
setWritable
method specifies the permissions on write operations to aFile
object. This method is overloaded as follows:
setWritable(boolean writable)
setWritable(boolean writable, boolean ownerOnly)
The first method is used to specify if the file is writable or not. The second method enhances the first by specifying if the write permission is applicable only to the owner. setReadable()
- The
setReadable
method specifies the permissions on read operations to aFile
object. This method is overloaded as follows:
setReadable(boolean readable)
setReadable(boolean readable, boolean ownerOnly)
The first method specifies if the file can be accessed or not. The second method enhances the first by specifying if the read permission is applicable only to the owner.
If the underlying file system cannot distinguish the owner's read permission from that of others, the permission will apply to everybody, regardless of this value. setExecutable():
- The
setExecutable
method specifies the execute permissions on aFile
object being executed. This method is overloaded as follows:
setExecutable(boolean executable)
setExecutable(boolean executable, boolean ownerOnly)
The method used to test if the file can be executed or not is thecanExecute()
method.
This code is an example of how
setWritable()
can be used.import java.io.*; public class SampleFileWrite { public static void main(String args[]) { if(args.length>0) { File f=new File(args[0]); f.setWritable(false); System.out.println("File writable? " + f.canWrite()); //checks if the File is editable f.setWritable(true, true); System.out.println("File writable? " + f.canWrite()); //checks if the File is editable } else { System.out.println(" Enter a Filename"); } } }
The
canWrite
method is used to check whether the file can be edited or is read only (where setWritable()
is set to false
).
2 comments:
nice article. I totally love the way you presented the topic. Hope to see you post soon again.
Selenium Training in Chennai
Superb posting.
Java course in Pune
Post a Comment