首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

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

2012-01-22 
控制台程序不会写请各位大侠帮我写注释啊 我看都看不懂 先谢了[codeJava][/code]package com.itjob.ioim

控制台程序不会写
请各位大侠帮我写注释啊 我看都看不懂 先谢了[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() + "> "; } } 


[解决办法]

Java code
// 累了....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() + "> "; //返回此抽象路径名的绝对路径名字符串.    } } 

热点排行
Bad Request.