import java.io.*;

/**
 * @Description:
 * @projectName:JavaTest
 * @see:PACKAGE_NAME
 * @author:郑晓龙
 * @createTime:2019/5/2 22:41
 * @version:1.0
 */
public class CopyFile {
    public static void main(String[] args) {
        copy("d:/abc.txt","d:/def.txt");
    }

    public static void copy(String source, String target) {
        // 1、创建源
        File src = new File(source);
        File dest = new File(target);

        // 2、选择流
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(src);
            os = new FileOutputStream(dest);
            
            // 3、操作流
            byte[] buf = new byte[1024];
            int len;
            while ((len = is.read(buf)) != -1) {
                os.write(buf, 0, len);
            }
            os.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4、关闭流(先打开的后关闭)
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

  

相关文章:

  • 2021-04-17
  • 2021-06-29
  • 2022-12-23
  • 2022-01-09
  • 2021-12-02
  • 2021-04-10
  • 2022-12-23
  • 2018-11-04
猜你喜欢
  • 2021-07-09
  • 2022-02-17
  • 2022-01-05
  • 2021-06-25
  • 2021-10-29
  • 2021-09-29
  • 2022-12-23
相关资源
相似解决方案