close

1. 操作這兩個類別需要try-catch

2. 只要有開啟檔案,程式最後一定要關閉檔案

3.寫檔案有兩種,一種破壞原本檔案直接覆蓋過去;另一種不破壞檔案複寫過去

4.更詳細的操作需要查詢文件 java.sun.com


●由程式操作類別,拷貝檔案:

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Test0506 {

    public static void main(String[] args) {
        
        FileInputStream fis = null;
        FileOutputStream fos = null;
        
        try{
            fis = new FileInputStream("/aa.txt");
            fos = new FileOutputStream("/bb.txt");                //若加上 ,true 就不會破壞檔案
                   int data = 0;
            while( (data = fis.read()) != -1){
                fos.write(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                fis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }  
     }
}
 

arrow
arrow

    淺翔 發表在 痞客邦 留言(0) 人氣()