项目借鉴

https://blog.csdn.net/u012903926/article/details/45967767

package com.mvc.test;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCopyTest {

    public static void main(String[] args) {





        String filePath="D:\\poi\\test.xls";
        File file = new File(filePath);

        File file1 =new File("D:\\poi\\test");
        if(!file1.exists()){
            file1.mkdir();
        }


        copyFile(filePath,"D:\\poi\\test");
        try {
            FileInputStream in = new FileInputStream(file);

            BufferedInputStream bi = new BufferedInputStream(in);

            File file2 = new File("D:\\poi\\test\\test.xls");

            FileOutputStream out = new FileOutputStream(file2);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


     private static void copyFile(String srcPathStr, String desPathStr) {
         //1.获取源文件的名称
         String newFileName = srcPathStr.substring(srcPathStr.lastIndexOf("\\")+1); //目标文件地址
         System.out.println(newFileName);
         desPathStr = desPathStr + File.separator + newFileName; //源文件地址
         System.out.println(desPathStr);

         try{
             //2.创建输入输出流对象
             FileInputStream fis = new FileInputStream(srcPathStr);
             FileOutputStream fos = new FileOutputStream(desPathStr);                

             //创建搬运工具
             byte datas[] = new byte[1024*8];
             //创建长度
             int len = 0;
             //循环读取数据
             while((len = fis.read(datas))!=-1){
                 fos.write(datas,0,len);
             }
             //3.释放资源
             fis.close();
             fis.close();
         }catch (Exception e){
             e.printStackTrace();
         }
     }

}

猜你喜欢

转载自blog.csdn.net/jiangwudidebaba/article/details/82119878