File.renameToでファイルを移動

2009/02/16 18:42Update
TAGS: File | ファイル | 移動

Javaでファイルを移動するサンプルです。

■コード
    public static boolean moveFile(String orgFilePath, String destDir) {
        // 移動もとなるファイルパス
        File file = new File(orgFilePath);
        
        // 移動先ディレクトリ
        File dir = new File(destDir);
        
        // 移動
        return file.renameTo(new File(dir, file.getName()));
    }


■使い方
moveFile("c:\\hello.txt", "c:\new\\")

c:\\hello.txtファイルをc:\new\\に移動します。

有关作者
Syboos.jp編集長AJavaやオープンソース情報の執筆、Webサイトの開発や運営全般の業務に携わる。

Sponsored Link


Comments

用户名 (required)

Email (will not be published) (required)

URL

Evaluation