疯狂java讲义习题11.5

xiaoxiao2021-02-28  83

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

点击(此处)折叠或打开

import java.io.*; import java.util.Scanner; public class cmd {      public static void showAllFiles(String path)      {      File newFile = new File(path);      if (newFile.exists())      {      File[] fileList = newFile.listFiles();      if (fileList.length == 0)      {      System.out.println("文件夹是空的");      }           for (File file:fileList)      {      if (file.isFile())      {      System.out.println("文件名"+file.getAbsolutePath());      }      else{      System.out.println("路径名"+file.getAbsolutePath());      showAllFiles(file.getAbsolutePath());      }      }      }      else      System.out.println("文件不存在");      }      public static void copyFile(String dst,String src) throws IOException      {      FileOutputStream fos = null;      FileInputStream fis = null;      fos = new FileOutputStream(dst+"\\"+src.substring(src.lastIndexOf('\\')+1));      fis = new FileInputStream(src);      int hasRead = 0;      byte[] bbuf = new byte[1024];      while ((hasRead = fis.read(bbuf)) > 0)      {           fos.write(bbuf,0,hasRead);      }           }      public static void copy(String dst,String src) throws IOException      {      File newFile = new File(src);      File[] fileList = newFile.listFiles();      //File dstFile = new File(dst+"//"+newFile.getName());      if (newFile.isFile())      {           copyFile(dst,src);      }      else      {      File dstFile = new File(dst+"//"+newFile.getName());      if (!dstFile.exists())      {      dstFile.mkdirs();      }      for (File file:fileList)      {      if (file.isFile())      copyFile(dstFile.getAbsolutePath(),file.getAbsolutePath());      else      copy(dstFile.getAbsolutePath(),file.getAbsolutePath());      }      }      }     public static void main(String[] args) throws IOException {         InputStreamReader reader = new InputStreamReader(System.in);         BufferedReader br = new BufferedReader(reader);         String line = null;         while ((line = br.readLine()) != null)         {             //System.out.println(line[0]);              String ss[] = line.split(" ");//用Scanner无法处理\n             // char c = ss[0].charAt(0);              if (ss.length > 3)              {                                   return;              }              if (ss[0].equals("move"))              {                                   copy(ss[1],ss[2]);                  System.out.println("OK");              }         /*     else if ((c =='c' || c == 'C') ||                      (c == 'd' || c == 'd'))                               {                  System.out.println(c+"\\>");                               }*/              else if (ss[0].equals("dir"))//文件名不能有空格              {                  System.out.println(ss[1]);                  showAllFiles(ss[1]);                               }              else              {                  System.out.println(ss[0]);                  System.out.println("hi");              }         }                                    } } /*import java.io.*; public class cmd {  public static void main(String args[]) throws IOException  {   System.out.print("请输入回车键继续...");   if (new InputStreamReader(System.in).read() == 13)   {    //Do something here;   }  } }*/ <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script> 阅读(65) | 评论(0) | 转发(0) | 0

上一篇:疯狂java讲义习题11.4

下一篇:疯狂java讲义习题7.1

相关热门文章 Tomcat 6 配置SSI让Resin支持shtml(SSI)- 静...tomcat + ssiASP JavaScript Lessons(8-14)JDK1.6官方下载_JDK6官方下载_... 给主人留下些什么吧!~~ 评论热议
转载请注明原文地址: https://www.6miu.com/read-59681.html

最新回复(0)