[Java]讀取InputRead及寫入OutputWrite檔案File

程式碼如下:

package yku.io;

import java.io.File;

public class FileTool {
    //讀取檔案
    public static String Reader(String pathName) throws java.io.IOException {
        java.io.FileReader reader = new java.io.FileReader(pathName);
        // 承載讀取內容的buff
        StringBuffer buff = new StringBuffer();
        //一次讀取256個字元
        char[] c = new char[256];
        int length;
        while ((length = reader.read(c)) > 0) {
            buff.append(c);
        }
        reader.close();
        return buff.toString();
    }
    //寫入檔案,把指定的data寫入檔案
    public static void Writer(String pathName, String data)
            throws java.io.IOException {
        java.io.FileWriter writer = new java.io.FileWriter(pathName);
        // 寫入檔案
        writer.write(data);
        writer.close();
    }
    //複制,指定來源及目地,利用FileReader及FileWriter來讀及寫
    public static void copy(String FromPath, String ToPath)
            throws java.io.IOException {
        java.io.FileReader reader = new java.io.FileReader(FromPath);
        java.io.FileWriter writer = new java.io.FileWriter(ToPath);
        char[] c = new char[256];
        int length;
        while ((length = reader.read(c)) > 0) {
            writer.write(c, 0, length);
        }
        reader.close();
        writer.close();
    }

    // 刪除檔案
    public static void delete(String filepath) throws java.io.IOException {
        java.io.File f = new java.io.File(filepath);
        //System.out.println(filepath+":"+f.exists());
        f.deleteOnExit();
    }

    // 移動檔案,copy後刪除原本檔案
    public static void movie(String FromPath, String ToPath)
            throws java.io.IOException {
        copy(FromPath, ToPath);
        delete(FromPath);
    }
    //先讀檔案後存入記憶體,再一次寫入檔案,
    //當需要把讀取的內容做處理時可以這麼做
    public static void copy2(String FromPath, String ToPath)
            throws java.io.IOException {
        // 這個效率不如copy
        Writer(ToPath, Reader(FromPath));
    }

    public static void main(String args[]) throws java.io.IOException {
        // 取出class目錄下的text.txt檔案
        String pathName = FileTool.class.getResource("/text.txt").getFile();
        // System.out.println(pathName);
        // 取得目錄
        java.io.File f = new java.io.File(pathName);
        String path = f.getParent();
        // System.out.println(path);

        System.out.println("讀出資料");
        // 讀出資料
        String str = FileTool.Reader(pathName);
        System.out.println(str);
        
        
        System.out.println();
        
        
        
        System.out.println("寫入資料");
        // 寫入資料
        String writeName = path + File.separator + "write.txt";
        FileTool.Writer(writeName, "測試看看有沒有成功,123\r\n中文abcde");
        System.out.println("寫入成功:" + writeName);

        System.out.println();
        
        System.out.println("copy");
        // copy
        String copyName = path + File.separator + "copy.txt";
        FileTool.copy(pathName, copyName);
        System.out.println("copy成功:" + copyName);

        System.out.println();
        
        
        System.out.println("copy2");
        // copy2
        String copyName2 = path + File.separator + "copy2.txt";
        FileTool.copy(pathName, copyName2);
        System.out.println("copy2成功:" + copyName2);

        System.out.println();
        
        
        System.out.println("movie:移動的檔案名稱:" + pathName);
        // copy2
        String mvName = path + File.separator + "mv.txt";
        FileTool.movie(pathName, mvName);
        System.out.println("移動成功,原本檔案將被刪除:" + mvName);

    }
}

3 thoughts to “[Java]讀取InputRead及寫入OutputWrite檔案File”

  1. 大大的教學好多 都很實用 (拍拍手~~~

    湊一湊都可以出一本程式書了:D

發表迴響