java 实现指定文件的复制和 拷贝

package com;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;

public class DoFile {
    
    public  boolean PrintPage(String comePage,String comeFile,String toPage,String toFile) throws IOException {
    boolean flag=false;
    FileInputStream fis=null;
    FileOutputStream fos=null;
    
    System.out.println("源文件夹 地址:"+comePage);
    System.out.println("您要复制的文件是: "+comeFile);
    System.out.println("目标文件夹地址: "+toPage);
    System.out.println("新生成的文件:"+toFile);
    try {
        //判断源文件夹是否存在,是否yi /结尾,如果没有,要进行添加
        if(comePage.endsWith("/")){//继续执行,不必改变
            System.out.println("是以 / 结尾的");
        }
        else{
            System.out.println("您输入的源文件夹最后应以/ 结尾,不过没关系,系统帮您加上了");
            StringBuffer newComePage=new StringBuffer(comePage);
            newComePage.append("/");
            System.out.println("newComePage: "+newComePage);
            comePage=newComePage.toString();
        }
        //判断文件是否存在,不存在,就反馈给用户
        File ff=new File(comePage+comeFile);
        if(ff.exists()){
            System.out.println("存在这个文件");
        }
        else{
            System.out.println("您指定的文件不存在");
            return false;
        }
        
       //判断源文件夹是否存在,是否yi /结尾,如果没有,要进行添加
        System.out.println("目标文件夹 地址:"+toPage);

        if(toPage.endsWith("/")){//继续执行,不必改变
            System.out.println("是以 / 结尾的");
        }
        else{
            System.out.println("您输入的源文件夹最后应以/ 结尾,不过没关系,系统帮您加上了");
            StringBuffer newComePage=new StringBuffer(toPage);
            newComePage.append("/");
            System.out.println("newComePage: "+newComePage);
            toPage=newComePage.toString();
            
        }
        
        //判断目标文件夹是否存在,不存在就创建
        File dd=new File(toPage);
        if(dd.exists()){
            System.out.println("目标文件夹存在,不必创建");
        }
        else{
            System.out.println("目标文件夹不存在,不过,别担心,系统帮您创建了");
            dd.mkdirs();
        }
        //进行文件拷贝
        fis=new FileInputStream(comePage+comeFile);
        fos=new FileOutputStream(toPage+toFile);
        
        //中转数组
        byte[] buff=new byte[1024];
        int readed=-1;
        while((readed=fis.read(buff))>0)
            fos.write(buff, 0, readed);
        
        flag=true;
    } catch (FileNotFoundException e) {
        System.out.println("请确切源文件夹中存在您指定的文件");
        e.printStackTrace();
        flag=false;
    }
    finally{
        
        fis.close();
        fos.close();
        return flag;
    }
      
    
     }

     public static void main(String[] args) throws IOException {
         System.out.println("begin");

      
        
         DoFile df=new DoFile();
     df.PrintPage("f:/source","old.txt","f:/now/","new.txt");
           System.out.println("success");
     }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值