JDK6.0学习笔记(六)复制文件

 
  1. /**
  2.  * 复制文件
  3.  * 运行命令行 java CopyFile C:/1.txt D:1.txt
  4.  * */
  5. import java.io.*;
  6. import java.text.*;
  7. import java.util.*;
  8. public class CopyFile {
  9.     public static void main(String[] args) throws IOException {
  10.         new CopyFile().copy(args[0], args[1], Integer.parseInt(args[2]));
  11.     }
  12.     public int copy(String source_name, String dest_name, int type)
  13.             throws IOException {
  14.         File source_file = new File(source_name);
  15.         File dest_file = new File(dest_name);
  16.         FileInputStream source = null;
  17.         FileOutputStream destination = null;
  18.         byte[] buffer;
  19.         int bytes_read;
  20.         int result = 0;
  21.         try {
  22.             if (!source_file.exists() || !source_file.isFile())
  23.                 throw new RuntimeException("FileCopy: no such source file: "
  24.                         + source_name);// 源文件不存在
  25.             if (!source_file.canRead())
  26.                 throw new RuntimeException("FileCopy: source file "
  27.                         + "is unreadable: " + source_name);// 源文件不可读
  28.             if (dest_file.exists()) {
  29.                 if (dest_file.isFile()) {
  30.                     if (type == 1// 覆盖
  31.                     {
  32.                         dest_file.delete();
  33.                         result = 1;
  34.                     } else // 不覆盖
  35.                     {
  36.                         result = 2;
  37.                         return result;
  38.                     }
  39.                 } else
  40.                     throw new RuntimeException("FileCopy: destination "
  41.                             + "is not a file: " + dest_name);// 目标是目录而不是文件
  42.             } else {
  43.                 File parentdir = new File(dest_file.getParent());
  44.                 if (!parentdir.exists())
  45.                     throw new RuntimeException("FileCopy: destination "
  46.                             + "directory doesn't exist: " + dest_name);// 目标路径不存在
  47.                 if (!parentdir.canWrite())
  48.                     throw new RuntimeException("FileCopy: destination "
  49.                             + "directory is unwriteable: " + dest_name);// 目标路径不可写
  50.             }
  51.             // 复制文件
  52.             source = new FileInputStream(source_file);
  53.             destination = new FileOutputStream(dest_file);
  54.             buffer = new byte[1024];
  55.             while (true) {
  56.                 bytes_read = source.read(buffer);
  57.                 if (bytes_read == -1) {
  58.                     break;
  59.                 }
  60.                 destination.write(buffer, 0, bytes_read);
  61.             }
  62.         } finally {
  63.             if (source != null) {
  64.                 try {
  65.                     source.close();
  66.                 } catch (IOException e) {
  67.                 }
  68.             }
  69.             if (destination != null) {
  70.                 try {
  71.                     destination.close();
  72.                 } catch (IOException e) {
  73.                 }
  74.             }
  75.             return result;
  76.         }
  77.     }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值