Java实现文件的复制

编写一个 Java 程序,能将硬盘上某个文件夹下的一个文件拷贝到另一个指定的文件夹中。

import java.io.*;

class fileOperate_copy{
    public fileOperate_copy(){
        byte buf[]= new byte[100000000]; //定义一个100000000个字节的缓冲区
        //以保证能够缓存文件中所有内容
        try
        {
            FileInputStream fis= new FileInputStream("d:/file1.txt"); //注意不能用“\”
            int rd=fis.read(buf,0,100000000); //将文件内容读取到字节缓冲区中
            String str= new String(buf,0,rd); //将缓冲区内容转换为字符串
          //System.out.println(str); //输出转换为字符串后的文件
            FileOutputStream fos= new FileOutputStream("d:/file3.txt");
            fos.write(buf,0,rd);
            fis.close(); //关闭流
        }
        catch(IOException e)
        {
            System. out .println("File read Error");
        }
    }
}

public class test2 
{
    public test2(){
        fileOperate_copy f = new fileOperate_copy();
    }
    public static void main(String args[])
    {
         test2 t = new test2();
    }
}

运行结果:
在这里插入图片描述

打开file3就会发现里面的内容和file1中的内容完全相同。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值