文件的复制

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中的字节流用于处理字节的输入输出。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章