0
点赞
收藏
分享

微信扫一扫

疯狂java讲义习题11.5


题目描述:编写一个命令行工具,能够像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());
(file.getAbsolutePath());
}
} 
}
else
System.out.println("文件不存在");
}
public static void copyFile(String dst,String src) throws IOException
{
FileOutputStream fos = null;
FileInputStream fis = null; 
= new FileOutputStream(dst+"\\"+src.substring(src.lastIndexOf('\\')+1));
= new FileInputStream(src);
int hasRead = 0;
byte[] bbuf = new byte[1024];
while ((hasRead = fis.read(bbuf)) > 0)
{
      
.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())
{
      
(dst,src);
}
else
{
File dstFile = new File(dst+"//"+newFile.getName());
if (!dstFile.exists())
{
.mkdirs();
}
for (File file:fileList)
{
if (file.isFile())
(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]);
(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;
   }
  }
 }*/



举报

相关推荐

0 条评论