Mar 20, 2006

[Mustang] Mustang における File の 追加機能

Mustang では java.io.File も結構機能追加されていることに気付いた。 ディスクスペースの取得や、パーミッション関連の操作が追加されている。

Class File
http://download.java.net/jdk6/docs/api/java/io/File.html

Mustang で File に追加されたメソッド

public boolean setWritable(boolean writable,boolean ownerOnly)
Sets the owner's or everybody's write permission for this abstract pathname.
public boolean setWritable(boolean writable)
A convenience method to set the owner's write permission for this abstract pathname.
public boolean setReadable(boolean readable,boolean ownerOnly)
Sets the owner's or everybody's read permission for this abstract pathname.
public boolean setReadable(boolean readable)
A convenience method to set the owner's read permission for this abstract pathname.
public boolean setExecutable(boolean executable,boolean ownerOnly)
Sets the owner's or everybody's execute permission for this abstract pathname.
public boolean setExecutable(boolean executable)
A convenience method to set the owner's execute permission for this abstract pathname.
public boolean canExecute()
Tests whether the application can execute the file denoted by this abstract pathname.
public long getTotalSpace()
Returns the size of the partition named by this abstract pathname.
public long getFreeSpace()
Returns the number of unallocated bytes in the partition named by this abstract path name.
public long getUsableSpace()
Returns the number of bytes available to this virtual machine on the partition named by this abstract pathname.

サンプルコード(ディスクスペースの取得)

package jp.in_vitro.codelets.io;

import java.io.File;

public class Codelet {

    public Codelet(){
        super();
    }
    
    public static void main(String[] args) {
        Codelet me = new Codelet();
        me.execute();
    }
    
    protected void execute(){
        File file = new File("c:\\");
        System.out.println("" + file.getAbsolutePath());
        System.out.println("FREE SPACE   : " + file.getFreeSpace());
        System.out.println("TOTAL SPACE  : " + file.getTotalSpace());
        System.out.println("USABLE SPACE : " + file.getUsableSpace());
    }
}

実行結果

c:\
FREE SPACE   : 2151354368
TOTAL SPACE  : 15002906624
USABLE SPACE : 2151354368