当前位置: 代码迷 >> J2SE >> 控制台程序不会写,该如何处理
  详细解决方案

控制台程序不会写,该如何处理

热度:67   发布时间:2016-04-24 13:07:08.0
控制台程序不会写
请各位大侠帮我写注释啊 我看都看不懂 先谢了[code=Java][/code]
package com.itjob.io;

import java.io.*;

public class DataStreamDemo {

static String path;
static String[] cmds = null;
/**
* @param args
* @throws IOException 
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputStream in = System.in;

String cmd = "";
File f = new File(System.getProperty("user.dir"));
BufferedReader read = new BufferedReader(new InputStreamReader(in));

//StreamTokenizer st = new StreamTokenizer(new InputStreamReader(in));


path = getAbsolutePath(f);
while (!cmd.equals("exit"))
{
System.out.print(path);
cmd = read.readLine().trim();
cmds = cmd.split(" ");
if (cmds.length == 2 && cmds[0].equals("cd") && cmds[1] != null)
{

if (cmds[1].equals(".."))
{
if (f != null && f.getParentFile() != null)
f = f.getParentFile();
}
else
{
System.setProperty("user.dir", f.getAbsolutePath());
f = new File(new File(cmds[1]).getAbsolutePath());
}


path =getAbsolutePath(f.isDirectory() ? f : null);
}


if (cmds.length == 1 && cmds[0].equals("ls"))
{
for (String name : f.list())
{
System.out.print(name + " ");
}
System.out.println();
}

if (cmds.length == 3 && cmds[0].equals("cp") 
&& cmds[1] != null && cmds[2] != null)
{
File src = new File(cmds[1]);
File dest = new File(cmds[2]);

if (src.exists() && dest.exists()
&& src.isFile() && dest.isDirectory())
{
BufferedReader bread = new BufferedReader(new InputStreamReader(
new FileInputStream(src)));

BufferedWriter bwrite = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(dest.getPath() + File.separator + src.getName())));

String line = null;
while ((line = bread.readLine()) != null)
{
bwrite.append(line);
bwrite.newLine();
}
bwrite.flush();

bread.close();
bwrite.close();
}
else
{
System.out.println("No such file or directory: \"" + cmds[1] + "\" and \"" + cmds[2] + "\"");
}
}
}


//random.close();
}

static String getAbsolutePath(File file)
{
if (file == null)
{
System.out.println("No such file or directory: " + cmds[1]);
return path;

}

return file.getAbsolutePath() + "> ";
}

}


------解决方案--------------------
Java code
package com.itjob.io; import java.io.*; public class DataStreamDemo { static String path; //路径名static String[] cmds = null; //命令列表/**  * @param args  * @throws IOException   */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub InputStream in = System.in;// 获得控制台输入流String cmd = ""; File f = new File(System.getProperty("user.dir")); //获得用户当前所在目录BufferedReader read = new BufferedReader(new InputStreamReader(in)); //从控制台输入流构造一个字符缓冲区输入流//StreamTokenizer st = new StreamTokenizer(new InputStreamReader(in)); path = getAbsolutePath(f); //取得用户当前所在目录的绝对路径while (!cmd.equals("exit")) { System.out.print(path); //向控制台输出用户当前所在目录的绝对路径cmd = read.readLine().trim(); //从控制台读取一行用户输入的命令cmds = cmd.split(" "); //把从控制台读取一行用户输入的命令以空格" "分离成字符串,存在数组cmds中if (cmds.length == 2 && cmds[0].equals("cd") && cmds[1] != null) { //如果用户输入的一行命令长度为2同时命令的第一个字符串是"cd"同时"cd"后面不是空的就执行下面的程序if (cmds[1].equals("..")) { //如果命令的第一个参数是"..",当前目录不是空,就用f存放父目录if (f != null && f.getParentFile() != null) f = f.getParentFile(); } else //如果命令的第一个参数不是是"..",就设置系统的当前目录为参数的绝对目录{ System.setProperty("user.dir", f.getAbsolutePath()); f = new File(new File(cmds[1]).getAbsolutePath()); } //执行完命令后,如果path =getAbsolutePath(f.isDirectory() ? f : null); //把第一个参数表示的路径存到path里} if (cmds.length == 1 && cmds[0].equals("ls"))//如果命令行字符串长度等于1,同时命令是"ls" { for (String name : f.list()) //遍利当前目录下所有文件夹和文件,并输出名称{ System.out.print(name + "  "); } System.out.println(); } if (cmds.length == 3 && cmds[0].equals("cp")  && cmds[1] != null && cmds[2] != null) //如果命令是"cp"同时第一个和第二个参数都不为空{ File src = new File(cmds[1]); //src 为第一个参数, 源文件File dest = new File(cmds[2]);//dest为第二个参数,目标文件夹if (src.exists() && dest.exists() && src.isFile() && dest.isDirectory()) { //如果源文件和目标文件夹都存在,就把文件复制到目标文件夹,下面的代码都是这功能BufferedReader bread = new BufferedReader(new InputStreamReader( new FileInputStream(src))); BufferedWriter bwrite = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(dest.getPath() + File.separator + src.getName()))); String line = null; while ((line = bread.readLine()) != null) { bwrite.append(line); bwrite.newLine(); } bwrite.flush(); bread.close(); bwrite.close(); } else { //如果源文件或目标文件夹不存在,就输出错无信息System.out.println("No such file or directory: \"" + cmds[1] + "\" and \"" + cmds[2] + "\""); } } } //random.close(); } static String getAbsolutePath(File file) //静态函数,获得绝对路径{ if (file == null) { System.out.println("No such file or directory: " + cmds[1]); return path; } return file.getAbsolutePath() + "> "; } }
  相关解决方案