文件的复制

来源:互联网 发布:python 字符串字节数 编辑:程序博客网 时间:2024/06/06 18:19
import java.io.*;import java.util.Scanner;public class fuzhi {    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();        }    }}

运行结果如下:
这里写图片描述