设计一程序(Copy.java),可以实现文件的复制操作
要求:模仿DOS的copy命令,输入两路径名则实现文件拷贝功能。若输入以下命令Java copy f:\\1.txt f:\\2.txt 则完成把f:\\1.txt的内容复制到f:\\2.txt中。
提示:可以使用File、FileInputStream、FileOutputStream实现
package com.cmm.io4; import java.io.*; public class Copy { /** * @param arg * @Peter Cao */ public static void main(String[] args) { // TODO Auto-generated method stub String filePath1 =""; String filePath2 = ""; File file1 = null; File file2 = null; byte[] bt = new byte[1024]; if (args.length == 2) { filePath1 = args[0]; filePath2 = args[1]; file1 = new File(filePath1); file2 = new File(filePath2); if (file1.exists() && file1.isFile()) { try { // FileInputStream -- BufferedInputStream FileInputStream fins = new FileInputStream(file1); BufferedInputStream bins = new BufferedInputStream(fins); // BufferedInputStream bins = new BufferedInputStream(new FileInputStream(file1)); // FileOutputStream -- BufferedOutputStream FileOutputStream fouts = new FileOutputStream(file2); BufferedOutputStream bouts = new BufferedOutputStream(fouts); if (!file2.exists()) { file2.createNewFile(); // 若文件不存在,就创建文件 } while (bins.read(bt) > 0) // or bins.read(bt) != -1 { bouts.write(bt); bouts.flush(); bt = new byte[1024]; // refresh byte[] } bouts.close(); fouts.close(); bins.close(); fins.close(); } catch (Exception e) { e.printStackTrace(); } } } } }