Chinaunix首页 | 论坛 | 博客
  • 博客访问: 504497
  • 博文数量: 184
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1172
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-21 13:40
个人简介

技术改变命运

文章分类

全部博文(184)

文章存档

2020年(16)

2017年(12)

2016年(156)

我的朋友

分类: Java

2016-12-18 00:34:29

题目描述:编写一个命令行工具,能够像Windows提供的cmd命令一样,可以执行各种常见的命令,如dir,move等
代码如下:(功能有待完善)

点击(此处)折叠或打开

  1. import java.io.*;
  2. import java.util.Scanner;
  3. public class cmd {
  4.      public static void showAllFiles(String path)
  5.      {
  6.      File newFile = new File(path);
  7.      if (newFile.exists())
  8.      {
  9.      File[] fileList = newFile.listFiles();
  10.      if (fileList.length == 0)
  11.      {
  12.      System.out.println("文件夹是空的");
  13.      }
  14.     
  15.      for (File file:fileList)
  16.      {
  17.      if (file.isFile())
  18.      {
  19.      System.out.println("文件名"+file.getAbsolutePath());
  20.      }
  21.      else{
  22.      System.out.println("路径名"+file.getAbsolutePath());
  23.      showAllFiles(file.getAbsolutePath());
  24.      }
  25.      }
  26.      }
  27.      else
  28.      System.out.println("文件不存在");
  29.      }
  30.      public static void copyFile(String dst,String src) throws IOException
  31.      {
  32.      FileOutputStream fos = null;
  33.      FileInputStream fis = null;
  34.      fos = new FileOutputStream(dst+"\\"+src.substring(src.lastIndexOf('\\')+1));
  35.      fis = new FileInputStream(src);
  36.      int hasRead = 0;
  37.      byte[] bbuf = new byte[1024];
  38.      while ((hasRead = fis.read(bbuf)) > 0)
  39.      {
  40.     
  41.      fos.write(bbuf,0,hasRead);
  42.      }
  43.     
  44.      }
  45.      public static void copy(String dst,String src) throws IOException
  46.      {
  47.      File newFile = new File(src);
  48.      File[] fileList = newFile.listFiles();
  49.      //File dstFile = new File(dst+"//"+newFile.getName());
  50.      if (newFile.isFile())
  51.      {
  52.     
  53.      copyFile(dst,src);
  54.      }
  55.      else
  56.      {
  57.      File dstFile = new File(dst+"//"+newFile.getName());
  58.      if (!dstFile.exists())
  59.      {
  60.      dstFile.mkdirs();
  61.      }
  62.      for (File file:fileList)
  63.      {
  64.      if (file.isFile())
  65.      copyFile(dstFile.getAbsolutePath(),file.getAbsolutePath());
  66.      else
  67.      copy(dstFile.getAbsolutePath(),file.getAbsolutePath());
  68.      }
  69.      }
  70.      }
  71.     public static void main(String[] args) throws IOException {
  72.         InputStreamReader reader = new InputStreamReader(System.in);
  73.         BufferedReader br = new BufferedReader(reader);
  74.         String line = null;
  75.         while ((line = br.readLine()) != null)
  76.         {
  77.             //System.out.println(line[0]);
  78.              String ss[] = line.split(" ");//用Scanner无法处理\n
  79.             // char c = ss[0].charAt(0);
  80.              if (ss.length > 3)
  81.              {
  82.                 
  83.                  return;
  84.              }
  85.              if (ss[0].equals("move"))
  86.              {
  87.                 
  88.                  copy(ss[1],ss[2]);
  89.                  System.out.println("OK");
  90.              }
  91.         /*     else if ((c =='c' || c == 'C') ||
  92.                      (c == 'd' || c == 'd'))
  93.                 
  94.              {
  95.                  System.out.println(c+"\\>");
  96.                 
  97.              }*/
  98.              else if (ss[0].equals("dir"))//文件名不能有空格
  99.              {
  100.                  System.out.println(ss[1]);
  101.                  showAllFiles(ss[1]);
  102.                 
  103.              }
  104.              else
  105.              {
  106.                  System.out.println(ss[0]);
  107.                  System.out.println("hi");
  108.              }
  109.         }
  110.         
  111.             
  112.         
  113.     }
  114. }
  115. /*import java.io.*;

  116. public class cmd
  117. {
  118.  public static void main(String args[]) throws IOException
  119.  {
  120.   System.out.print("请输入回车键继续...");
  121.   if (new InputStreamReader(System.in).read() == 13)
  122.   {
  123.    //Do something here;
  124.   }
  125.  }
  126. }*/

阅读(1299) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~