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.     }

你可能感兴趣的:(C++,c,C#)