文件的複製

package wei;
import java.io.*;
import java.util.Scanner;
public class hklg {
    static final String INPUT="d:/student.txt";//定義輸入文件路徑
    static final String OUTPUT="d:/stunew.txt";//定義輸出文件路徑

    public static void main(String[] args) throws FileNotFoundException {
        // TODO Auto-generated method stub
        int iResult;
        String str;
        RandomAccessFile rdin=new RandomAccessFile(INPUT,"rw");//創建具有讀寫功能的RandomAccessFile對象rdin
        FileInputStream fisIn=new FileInputStream(INPUT);//創建文件讀入流對象fisIn
        FileOutputStream fosOut=new FileOutputStream(OUTPUT);//創建文件寫出流對象fosOut
        try{
            System.out.println("添加文件內容:");
            Scanner sc=new Scanner(System.in);
            str=sc.next();//從鍵盤輸入信息
            rdin.writeBytes(str);//將讀入的字符串信息寫入RandomAccessFile對象rdin
            System.out.println("開始複製文件"+INPUT);
            do{                          //實現複製功能
                iResult=fisIn.read();
                if(iResult!=-1){
                    fosOut.write(iResult);
                    System.out.println("...\n");
                }
            }while(iResult!=-1);
            System.out.println(INPUT+"已複製到"+OUTPUT);
            fisIn.close();//關閉fisIn對象
            fosOut.close();//關閉fosOut對象
        }
        catch(IOException e){
            e.printStackTrace();
        }

    }

}

這裏寫圖片描述

知識點:
1 字節流;
2 Input Stream類;
3 Output Stream類;
4 File Input Stream類;
5 File Output Stream類;

  我知道了字節流和字符流,java中的字節流用於處理字節的輸入輸出。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章