File Control Class


実行環境
OS:Microsoft Windows Me
 
ファイルコントロールクラス&各メソッドの作成
 zDelete:ファイルの削除
 zExists:ファイルの存在チェック
 zRenameTo:ファイルの名前変更(パスを変えれば移動)
 zCopy:ファイルの複写
 
ソースファイル:C:\java\FileControl.java
import java.lang.*;
import java.io.*;

public class FileControl {
    /**
    * FileControl コンストラクター・コメント。
    */
    public FileControl() {
        super();
    }
    public void zDelete(String inputfile) {
        File deleteFile = new File(inputfile);
        if (deleteFile.delete()) {
            System.out.print(deleteFile.getName() + " sucessfully deleted. ");
        } else {
            System.out.print("Could not delete " + deleteFile.getName() + ".");
        }
    }
    public boolean zExists(String inputfile) {
        File existFile = new File(inputfile);
        if (existFile.exists()) {
            System.out.print("Found " + existFile.getName() + ".");
            return true;
        } else {
            System.out.print("Not found " + existFile.getName() + ".");
            return false;
        }
    }
    public void zRenameTo(String inputfile, String outputfile) {
        File srcFile = new File(inputfile);
        File destFile = new File(outputfile);
        if (srcFile.renameTo(destFile)) {
            System.out.print(srcFile.getName() + " was renamed to " + destFile.getName());
        } else {
            System.out.print(srcFile.getName() + " was not renamed to " + destFile.getName());
        }
    }
    public void zCopy(String inputfile, String outputfile,boolean append) {
        BufferedReader reader = null;
        PrintWriter wtr = null;
        try {
            FileInputStream in = new FileInputStream(inputfile);
            reader = new BufferedReader(new InputStreamReader(in));
            FileOutputStream out = new FileOutputStream(outputfile,append);
            wtr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
            String copydata;
            while ((copydata = reader.readLine()) != null) {
                wtr.println(copydata);
            }
            System.out.print(inputfile + " was copy to " + outputfile);
        } catch (IOException e) {
            System.out.print(inputfile + " was not copy to " + outputfile);
            System.err.println(e);
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
                if (wtr != null) {
                    wtr.close();
                }
            } catch (IOException e) {
                System.err.println(e);
                e.printStackTrace();
            }
        }
    }
}
 
コンパイルファイル:C:\java\cpl.bat
C:\j2sdk1.4.0_01\bin\javac FileControl.java
pause
 
テストクラスの作成
ソースファイル:C:\java\HelloWorld.java
public class HelloWorld {
    /**
    * HelloWorld コンストラクター・コメント。
    */
    public HelloWorld() {
        super();
    }
    public static void main(String[] args) {
        System.out.println("Hello World!");

        //Exis Test
        FileControl existest = new FileControl();
        existest.zExists("c:\\java\\testa.txt");

        //Delete Test
        FileControl deletetest = new FileControl();
        deletetest.zDelete("c:\\java\\testb.txt");

        //Rename Test
        FileControl renametest = new FileControl();
        renametest.zRenameTo("c:\\java\\testc.txt","c:\\java\\testd.txt");

        //Copy Test Append true
        FileControl copytest = new FileControl();
        copytest.zCopy("c:\\java\\teste.txt","c:\\java\\testf.txt",true);

        //Copy Test Append false
        copytest.zCopy("c:\\java\\testg.txt","c:\\java\\testh.txt",false);

    }
}
コンパイルファイル:C:\java\cpl.bat
C:\j2sdk1.4.0_01\bin\javac HelloWorld.java
pause
 
テスト用テキストファイルの作成(テキストファイルの中身は適当にσ(^◇^;))
 C:\java\testa.txt 存在チェック用
 C:\java\testb.txt 削除用
 C:\java\testc.txt 置換用
 C:\java\teste.txt 複写用
 C:\java\testf.txt 複写用(追加)
 C:\java\testg.txt 複写用
 C:\java\testh.txt 複写用(上書)
 
実行ファイル:C:\java\run.bat
C:\j2sdk1.4.0_01\bin\java HelloWorld
pause
 
実行結果:DOSプロンプト
C:\java>C:\j2sdk1.4.0_01\bin\java HelloWorld
Hello World!
Found testa.txt.testb.txt sucessfully deleted. testc.txt was renamed to testd.tx
tc:\java\teste.txt was copy to c:\java\testf.txtc:\java\testg.txt was copy to c:
\java\testh.txt
C:\java>pause
続けるにはどれかキーを押してください . . .