ITeye博客

Java 仿 dos copy

windy2coast · 2007-11-02 20:26 · Markdown

java 代码

  1. import java.io.* ;
  2. public class Copy
  3. {
  4. public static void main(String args[])
  5. {
  6. if(args.length!=2)
  7. {
  8. System.out.println("请输入正确的格式的参数: (例如:java Copy d:\\\\input.txt d:\\\\output.txt") ;
  9. return ;
  10. }
  11. else
  12. {
  13. File file1 = new File(args[0]) ;
  14. File file2 = new File(args[1]) ;
  15. copyFile(file1,file2) ;
  16. }
  17. }
  18. public static void copyFile(File srcfile,File desfile)
  19. {
  20. byte[] b = new byte[512] ;//512可以改成其它大于零的适当整数值
  21. int len = 0 ;
  22. try
  23. {
  24. FileInputStream fis = new FileInputStream(srcfile) ;
  25. FileOutputStream fos = new FileOutputStream(desfile) ;
  26. while((len=fis.read(b))!=-1)//循环以512字节拷贝,知道拷贝完.
  27. {
  28. fos.write(b,0,len) ;
  29. }
  30. fis.close() ;
  31. fos.close() ;
  32. }
  33. catch(Exception e)
  34. {
  35. System.out.println(e) ;
  36. }
  37. }
  38. };

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。