IO流綜合實例(文件複製的相關操作)

   
   案例:分別在D盤、F盤下創建目錄aa、bb,並且在aa,bb目錄下分別創建a.txt、b.txt兩個文件,往這兩個文件中寫入數據,然後將兩個文件複製到E盤中,並且將E盤中的b.txt裏邊的內容複製到a.txt文件內容指定偏移量爲5處。
   分析:1.首先要創建目錄aa、bb,和原始文件a.txt和b.txt。
      2.寫入數據和複製文件兩個方法。
      3.寫入數據 字符高級緩衝流(自動刷新)println ,此時a.txt和b.txt有內容了。
      4.複製文件的過程是先創建E盤下a.txt和b.txt兩個文件,再往裏複製內容,此時E盤的兩個文件都複製成功。
      5.插入數據,把b內容插入a裏。
   注意:1.換行符的問題。2.File[i]數組建立。3.臨時文件的創建。
   
   

案例複習了IO流中的字符高級緩衝流、隨機訪問流中對象的創建,構造方法、有關方法的在文件操作的使用
對文件讀取、寫入、複製基本操作,臨時文件的創建

package zonghe_IO流總結;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;



public class IoStreamDemo {

    public static void main(String[] args) {
        IoStreamDemo file = new IoStreamDemo();
        file.createFiles();
    }

    /*
     * 創建文件
     */
    private void createFiles(){
        File file = new File("d:\\aa");
        File file0 =new File("f:\\bb");
        file.mkdir();//創建目錄aa
        file0.mkdir();//創建目錄bb
        File file1 = new File(file,"a.txt");
        File file2 = new File(file0,"b.txt");
        try {
            file1.createNewFile();//創建目錄aa下的文件a.txt
            file2.createNewFile();//創建目錄bb下的文件b.txt
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        write(file1,file2);//寫入數據
        copy(file1,file2);//複製文件
    }

    /*
     * 複製文件
     */
    private void copy(File file1, File file2) {
        //e盤創建目錄,和文件ee.txt
        File file = new File("e:"+File.separator+"ee");
        file.mkdir();//創建目錄ee

        File file3 = new File(file,"a.txt");//在目錄ee下創建文件a.txt
        File file4 = new File(file,"b.txt");//在目錄ee下創建文件b.txt

        try {
            file3.createNewFile();
            file4.createNewFile();
            //到此爲止已經有了4個文件了,還沒有複製
            File[] files =new File[4];
            files[0]=file1;
            files[1]=file2;
            files[2]=file3;
            files[3]=file4;
            //分別往e盤複製a.txt 和b.txt兩個文件
            for(int i=0;i<2;i++){
                copyAll(files[i],files[i+2]);
            }
            // 此時e盤裏邊有了a.txt 和b.txt 兩個文件  插入數據 
            insert(file3,file4);

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

    }

    /*
     * 插入數據 
     *   把b.txt插入到a.txt
     */
    private void insert(File file3, File file4) {
        try {
            RandomAccessFile raf1 = new RandomAccessFile(file3, "rw");
            File tmpFile = File.createTempFile("raf", ".tmp");//創建臨時文件
            RandomAccessFile raf2 = new RandomAccessFile(tmpFile, "rw");
            RandomAccessFile raf3 = new RandomAccessFile(file4, "rw");
            //將raf1的偏移量移到5,在把後面的內容複製到tmpFile
            raf1.seek(5);
            byte[] bys = new byte[1024];
            int len =0;
            while((len=raf1.read(bys))>0){
                raf2.write(bys, 0, len);
            }
            //raf1的偏移量重移至5
            raf1.seek(5);
            len =0;
            while((len=raf3.read(bys))>0){
                raf1.write(bys, 0, len);
            }

            //raf2的偏移量至回0
            raf2.seek(0);
            len=0;
            while((len=raf2.read(bys))>0){
                raf1.write(bys, 0, len);
            }
            //刪除臨時文件tmpFile
            tmpFile.deleteOnExit();
            //釋放資源
            raf1.close();
            raf2.close();
            raf3.close();
        } catch (FileNotFoundException e) {  //拋異常
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /*
     * 執行復制
     * 複製文件d盤複製到e
     */
    private void copyAll(File string, File string2) {

        try {
            //創建對象流,讀、寫流
            BufferedReader br =new BufferedReader(new InputStreamReader(new FileInputStream(string)));//字符緩衝輸入流,讀數據
            PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(string2)),true);//高級字符緩衝輸出流,寫數據,可以自動刷新

            //複製文件,先讀後寫
            String str =null;
            while((str=br.readLine())!=null){
                pw.println(str);
            }
            //釋放資源
            br.close();
            pw.close();
        } catch (FileNotFoundException e) {
            System.out.println("該文件未找到");
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    /*
     * 寫入數據
     */
    private void write(File file1, File file2) {
        //寫數據到a.txt
        try {
            //創建字符高級緩衝流
            PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file1)),true);//構造方法可以自動啓動刷新功能
            String[] strs = new String[1];
            for(int i=0;i<1;i++){
                strs[i]="abcdefghijklmnopqrstuvwxyz";
                pw.println(strs[i]);//往a.txt循環寫入數據
            }

            PrintWriter pw0 = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file2)),true);
            String str="ABC";
            pw0.print(str);//往b.txt文件裏寫入數據

            //釋放資源
            pw.close();
            pw0.close();
        } catch (FileNotFoundException e) {
            System.out.println("該文件未找到");
            e.printStackTrace();
        }


    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章