控制台程序不会写

hujing101 2008-04-29 09:10:29
请各位大侠帮我写注释啊 我看都看不懂 先谢了

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() + "> ";
}

}
...全文
105 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ericSola 2008-04-29
  • 打赏
  • 举报
回复
mark
youyou1225 2008-04-29
  • 打赏
  • 举报
回复
师姐啊!太厉害了!
anqini 2008-04-29
  • 打赏
  • 举报
回复

// 累了....

package zhao;
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; //System.in做为一个输入流

String cmd = "";
File f = new File(System.getProperty("user.dir")); //取得当前用户程序所在目录
BufferedReader read = new BufferedReader(new InputStreamReader(in)); //把输入流包装在BufferedReader

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


path = getAbsolutePath(f); //调用此方法,参数是上面得到的用户程序所在的目录,后面加上'>'符号
while (!cmd.equals("exit")) //假如从控制台读入的不是为"exit",就继续执行
{
System.out.print(path); //打印
cmd = read.readLine().trim(); //从控制台读入一行字符串
cmds = cmd.split(" "); //以" "来分割这个cmd字符串
if (cmds.length == 2 && cmds[0].equals("cd") && cmds[1] != null) //假如cmd为 cd d:\text 这种格式的话
{

if (cmds[1].equals("..")) //假如...这个你知道吧..呵呵
{
if (f != null && f.getParentFile() != null) //假如f不为null,它的上一目录不为null,意思就是它有上一目录
f = f.getParentFile(); //那么取它的上一目录
}
else
{
System.setProperty("user.dir", f.getAbsolutePath()); //把这个f目录的路径名字符串设置给这个值
f = new File(new File(cmds[1]).getAbsolutePath()); //取这个 cd 后面的目录的 绝对目录,比如说,你输入 cd test,那么返回这个test的绝对路径,再构件一个File对象
}


path =getAbsolutePath(f.isDirectory() ? f : null); //假如这个f对象不是文件,而是一个目录的话,就把这个f对象传进去,否则传null
}


if (cmds.length == 1 && cmds[0].equals("ls")) { //逻辑判断...

for (String name : f.list()) //遍历这个f对象目录底下的所有文件以及目录
{
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对象,好像这里涉及到linux命令,俺不懂。。。sorry
File dest = new File(cmds[2]);

if (src.exists() && dest.exists() //判断这个目录或者文件存不存在
&& src.isFile() && dest.isDirectory()) //判断src是否为文件,dest是否为目录
{
BufferedReader bread = new BufferedReader(new InputStreamReader(
new FileInputStream(src))); //假如src为文件的话,创建一个可以读取这个文件的输入流

BufferedWriter bwrite = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(dest.getPath() + File.separator + src.getName()))); //这个dest路径+"\"+src文件名字,创建一个可以输出到这个文件的输出流

String line = null;
while ((line = bread.readLine()) != null) //从控制台读入一行字符串
{
bwrite.append(line); //输出到这个bwrite输出流里
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() + "> "; //返回此抽象路径名的绝对路径名字符串.
}

}
KKK2007 2008-04-29
  • 打赏
  • 举报
回复

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() + "> ";
}

}

hujing101 2008-04-29
  • 打赏
  • 举报
回复
我想看看这个程序的注释 那位大哥帮忙注释一下

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧