Java文件复制

这是一个Java程序,用于将指定路径的文件从一个位置复制到另一个位置。它使用FileInputStream和FileOutputStream进行读写操作,实现了文件的移动。程序首先定义了输入和输出流对象,然后通过循环读取和写入数据来完成复制过程。
摘要由CSDN通过智能技术生成

文件夹复制

package cn.demo9;

import java.io.*;

public class Demo2 {
    private static FileInputStream fis =null;//创建输入流对象
    private static FileOutputStream fos=null;//创建输出流对象
    public static void main(String[] args)
    {
        String srcPathStr = "F:\\demo.txt"; //源文件地址
        String desPathStr = "F:\\Hello"; //目标文件地址
        copy(srcPathStr, desPathStr);//将F:\demo.txt文件拷贝到F:\Hello\\demo.txt
    }
    public static void copy(String srcPathStr, String desPathStr) {
        //获取源文件名
        File startFile = new File(srcPathStr);
        String startName=startFile.getName();
        System.out.println("源文件:"+startName);
        //目标地址desPathStr(desPathStr:F:\\Hello)+"\\"+原文件名
        desPathStr=desPathStr+"\\"+startName;
        System.out.println("目标文件地址:"+desPathStr);
        try {
           fis = new FileInputStream(srcPathStr);//创建输入流对象
            fos = new FileOutputStream(desPathStr); //创建输出流对象
            byte datas[] = new byte[1024*8];//创建搬运工具
            int len = 0;//创建长度
            while((len = fis.read(datas))!=-1)//循环读取数据
            {
                fos.write(datas,0,len);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                fis.close();//释放资源
                fis.close();//释放资源
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值