Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 355|回复: 0

[Java基础知识]模仿Dos界面的程序

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-2 08:21:26 | 显示全部楼层 |阅读模式
    /***********************************************
    * NETDEMO.CLASS (MAIN KERNEL OF "MOLECULE" VOS)
    *
    * WRITTEN BY "Fubin"
    * COMPILED IN JDK1.5.*
    * My MSN:yonghe169@163.com
    *
    *这是一个十分重要的模仿Dos界面的系统!非常重要。可以
    * 用在很多处理输入程序的地方
    *例如:Sisco等,可以写专门的接口工作程序
    ***********************************************/
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.net.*;
    import java.io.*;
    import java.text.*;  
      
      
    1. 运行图:
    2. [img]http://img.javaxxz.com/2014/10/2/082124828.gif[/img]

    3. public class NetDemo extends Panel implements KeyListener{
    4. static Frame myFrame;
    5. TextField tf;
    6. TextArea ta;
    7. Panel p1,p2,p3;
    8. Scrollbar sb1,sb2;
    9. String currentPath=System.getProperty("user.dir");
    10. String prompt=">",Temp="",drive_Name="";
    11. int commandNum=0,tempCommand=0;
    12. Object val;
    13. ArrayList ll=new ArrayList();       
    14. String currentContent="",path="";
    15. boolean editOn;
    16. public NetDemo() {
    17. ta=new TextArea(25,75);
    18. tf=new TextField(75);
    19. p1=new Panel();
    20. p1.setBackground(Color.lightGray);
    21. p2=new Panel();
    22. p2.setBackground(Color.lightGray);
    23. setLayout(new BorderLayout());
    24. p1.add(ta,BorderLayout.NORTH);
    25. add(p1,BorderLayout.NORTH);
    26. p2.add(tf,BorderLayout.NORTH);
    27. add(p2,BorderLayout.SOUTH);
    28. Font f=new Font("Monospaced",Font.PLAIN,15);
    29. ta.setFont(f);
    30. tf.setFont(f);
    31. ta.setBackground(Color.white);
    32. ta.setForeground(Color.black);
    33. tf.setBackground(Color.white);
    34. tf.setForeground(Color.black);
    35. ta.append("Title:       Molecule Virtual OS
    36. ");
    37. ta.append("Version:     Pre-beta Kernel, JVM version 1.2/1.3
    38. ");
    39. ta.append("Written by:  Amitava Sen
    40. ");
    41. ta.append("Contact:     write2sen@hotmail.com
    42. ");
    43. ta.append("Dated:       February, 2001.
    44. ");
    45. ta.append(currentPath+prompt);
    46. //ta.setEditable(true);
    47. tf.addKeyListener(this);
    48. requestFocus();
    49. tf.requestFocus();
    50. }
    51. void p(String s){ta.append(s);}
    52. public void keyTyped(KeyEvent ke) {};
    53.                
    54. public void keyReleased(KeyEvent ke) {};
    55. public void keyPressed(KeyEvent ke) {
    56.                
    57.                 if (editOn==false) commandControl(ke);
    58.                        
    59.                 if (editOn==true) edit(ke);
    60.                 }
    61. //////////////////////////////////////////////////
    62. ////////////////// EDIT MODE /////////////////////
    63. //////////////////////////////////////////////////
    64. protected void edit(KeyEvent ke) {
    65.                         editOn=true;
    66.                         char c = ke.getKeyChar();
    67.                         String commandEnter=tf.getText();                       
    68.                         StringTokenizer tokenizer=new StringTokenizer(commandEnter);
    69.        if (c=="
    70. ") {
    71.                
    72.                 if (commandEnter.equals("exit")) {
    73.                                 ta.setText(currentContent);
    74.                                 //ta.setEditable(false);
    75.                                 tf.setText("");
    76.                                 editOn=false;
    77.                                 ta.append(currentPath+prompt);
    78.                                 myFrame.setTitle("Molecule (NetDemo Console) - Type "help" or "?" for Help");
    79.                                 }
    80.                 else if (commandEnter.startsWith("save") || commandEnter.startsWith("save ")) {
    81.                                
    82.                         String token=tokenizer.nextToken();       
    83.                         String taContent=ta.getText();
    84.                         char opchar;
    85.                         int count,countLimit=taContent.length();
    86.                        
    87.                         while(tokenizer.hasMoreTokens()){
    88.                         try {                               
    89.                         token=tokenizer.nextToken();
    90.                                                                        
    91.                         FileOutputStream fout=new FileOutputStream(drive_Name+currentPath+"/"+token);
    92.                                                                
    93.                                 for (count=0;count< countLimit;count++){
    94.                                 opchar=taContent.charAt(count);
    95.                                 fout.write((int)opchar);
    96.                                
    97.                         }
    98.                         fout.close();
    99.                                        
    100.                         }catch(Exception e){
    101.                         ta.append("Error: Can"t save program ""+token+""
    102. ");
    103.                         }
    104.                         }               
    105.                        
    106.                         tf.setText("");
    107.                         tf.requestFocus();                       
    108.                 }       
    109.                         else {
    110.                                 tf.setText("");
    111.                                 ta.requestFocus();
    112.                         }
    113.                 }
    114.         }
    115. //////////////////////////////////////////////////
    116. /////////////// COMMAND MODE /////////////////////
    117. //////////////////////////////////////////////////
    118. protected void commandControl(KeyEvent ke) {
    119.         editOn=false;
    120.         char c = ke.getKeyChar();       
    121.         int key=ke.getKeyCode();
    122.                
    123.         if (key==KeyEvent.VK_UP) {
    124.                                         tempCommand=tempCommand-1;
    125.                                         try {
    126.                                         val=ll.get(tempCommand);
    127.                                         String commandSel=(String)val;
    128.                                         tf.setText("");
    129.                                         tf.setText(commandSel);
    130.                                         }catch(Exception e){}
    131.                                
    132.                                          }
    133.         if (key==KeyEvent.VK_DOWN) {
    134.                                         tempCommand=tempCommand+1;
    135.                                         try {
    136.                                         val=ll.get(tempCommand);
    137.                                         String commandSel=(String)val;
    138.                                         tf.setText("");
    139.                                         tf.setText(commandSel);
    140.                                         }catch(Exception e){}
    141.                                
    142.                                          }
    143.         if (c=="
    144. "){
    145.                
    146.                 String ta_Address="",command="";
    147.                 String commandEnter=tf.getText();
    148.                 commandEnter=commandEnter.trim();
    149.                 int i,commandMode;
    150.                 if (!commandEnter.equals("")) {
    151.                 ll.add(commandEnter);               
    152.                 commandNum=commandNum+1;
    153.                 tempCommand=tempCommand+1;       
    154.                 //System.out.println(ll+" "+commandNum);       
    155.                 }
    156.                
    157.                 try {       
    158.                 String tmp1=currentPath+"/"+commandEnter;
    159.                 String tmp2=tmp1.replace("","/");
    160.                 StringTokenizer st1=new StringTokenizer(tmp2);
    161.                 String tmp3=st1.nextToken();
    162.                 String tmp4=st1.nextToken();
    163.                 tmp2=tmp3+tmp4;
    164.                 //System.out.println(tmp2);
    165.                 }catch(Exception e){}       
    166.                 tf.setText("");
    167.                 StringTokenizer tokenizer=new StringTokenizer(commandEnter);
    168.                
    169.                 ////////////// IP ////////////////////
    170.                 if (commandEnter.startsWith("ip") || commandEnter.startsWith("ip "))
    171.                 {
    172.                 int ServerError=0;
    173.                 ta.setEditable(true);
    174.                 ta.append(commandEnter+"
    175. ");
    176.                 //ta.setEditable(false);
    177.                 String token=tokenizer.nextToken();
    178.                
    179.                 while(tokenizer.hasMoreTokens()){
    180.                         token=tokenizer.nextToken();
    181.                         try{
    182.                         InetAddress SW[]=InetAddress.getAllByName(token);
    183.                         for(i=0;i< SW.length;i++)
    184.                         ta_Address=ta_Address+SW[i]+"
    185. ";
    186.                         ta.setEditable(true);
    187.                         ta.append(ta_Address+"
    188. ");
    189.                         //ta.setEditable(false);
    190.                         }catch(Exception e)
    191.                         {
    192.                         ServerError=1;
    193.                         String cannotFindServer="";
    194.                         cannotFindServer=token;
    195.                         ta.append("Error: Can"t find server ""+cannotFindServer+""
    196. ");}
    197.                         }
    198.                         if (ServerError==1){
    199.                         ta.append(currentPath+prompt);
    200.                         tf.requestFocus();
    201.                         }
    202.                        
    203.                 }
    204.                
    205.                 ////////////// URL INFORMATION  //////////////////
    206.                 if (commandEnter.startsWith("urlinfo") || commandEnter.startsWith("urlinfo "))
    207.                 {
    208.                 ta.setEditable(true);
    209.                 ta.append(commandEnter+"
    210. ");
    211.                 //ta.setEditable(false);
    212.                 String token=tokenizer.nextToken();
    213.                
    214.                 while(tokenizer.hasMoreTokens()){
    215.                         token=tokenizer.nextToken();
    216.                         try{
    217.                         URL hp=new URL(token);
    218.                         URLConnection hpCon=hp.openConnection();
    219.                         ta.append("""+token+"" available data:
    220. ");
    221.                         ta.append("Date: "+new Date(hpCon.getDate())+"
    222. ");
    223.                         ta.append("Content/Type: "+hpCon.getContentType()+"
    224. ");
    225.                         ta.append("Last Modified: "+new Date(hpCon.getLastModified())+"
    226. ");
    227.                        
    228.                         ta.setEditable(true);
    229.                         ta.append(ta_Address+"
    230. ");
    231.                         //ta.setEditable(false);
    232.                         }catch(Exception e)
    233.                         {
    234.                         String cannotFindServer="";
    235.                         cannotFindServer=token;
    236.                         ta.append("Error: Can"t find server ""+cannotFindServer+""
    237. ");
    238.                         ta.append("Usage: urlinfo http://"+cannotFindServer+"
    239. ");}
    240.                         }
    241.                         ta.append(currentPath+prompt);
    242.                         tf.requestFocus();
    243.                                                
    244.                 }
    245.                
    246.                 ////////////// RUN ////////////////////
    247.                 else if (commandEnter.startsWith("run ") || commandEnter.startsWith("run"))
    248.                 {
    249.                 int runError=0;
    250.                 ta.setEditable(true);
    251.                 ta.append(commandEnter+"
    252. ");
    253.                 //ta.setEditable(false);
    254.                 String token=tokenizer.nextToken();
    255.                 String ta_Run="";
    256.                 while(tokenizer.hasMoreTokens()){
    257.                         token=tokenizer.nextToken();
    258.                         Runtime r=Runtime.getRuntime();
    259.                         Process p=null;
    260.                         try{
    261.                         p=r.exec(token);
    262.                         ta_Run=ta_Run+token+"
    263. ";
    264.                         ta.setEditable(true);
    265.                        
    266.                         }catch(Exception e){
    267.                         String cannotRunFile="";
    268.                         cannotRunFile=token;
    269.                         ta.append("Error: Can"t run program ""+cannotRunFile+""
    270. ");
    271.                         }
    272.                        
    273.                         }
    274.                         ta.append("The file(s) started are:
    275. "+ta_Run+"
    276. ");       
    277.                         ta.append(currentPath+prompt);
    278.                         //ta.setEditable(false);
    279.                 }
    280.                
    281.                                
    282.                 ////////////// WHOIS ////////////////////
    283.                 else if (commandEnter.startsWith("whois ") || commandEnter.startsWith("whois"))
    284.                 {
    285.                 int whois=0,ch;
    286.                 ta.setEditable(true);
    287.                 ta.append(commandEnter+"
    288. ");
    289.                 //ta.setEditable(false);
    290.                 String token=tokenizer.nextToken();
    291.                 String ta_Whois="",Whois="";
    292.                 while(tokenizer.hasMoreTokens()){
    293.                         token=tokenizer.nextToken();       
    294.                         try{
    295.                         Socket s=new Socket("internic.net",43);
    296.                         InputStream in=s.getInputStream();
    297.                         OutputStream out=s.getOutputStream();
    298.                         Whois=token+"
    299. ";
    300.                         byte buf[]=Whois.getBytes();
    301.                         out.write(buf);
    302.                         while ((ch=in.read())!=-1) {
    303.                         ta_Whois=ta_Whois+(char)ch;
    304.                         }
    305.                         ta.append(ta_Whois);
    306.                         s.close();
    307.                         }catch(Exception e){
    308.                         String cannotFindWhois="";
    309.                         cannotFindWhois=token;
    310.                         ta.append("Error: Can"t find server ""+cannotFindWhois+""
    311. ");
    312.                         }
    313.                         }
    314.                         //s.close();
    315.                         ta.append(currentPath+prompt);
    316.                         //ta.setEditable(false);
    317.                        
    318.                
    319.                 }
    320.                
    321.                 ////////////// FIND FILE /////////////
    322.                 else if (commandEnter.startsWith("find ") || commandEnter.startsWith("find"))
    323.                 {
    324.                 boolean findFile;
    325.                 ta.setEditable(true);
    326.                 ta.append(commandEnter+"
    327. ");
    328.                 String token=tokenizer.nextToken();
    329.                 String ta_FileExists="",ta_FileDoesNotExist="";
    330.        
    331.                 while(tokenizer.hasMoreTokens()){
    332.                         token=tokenizer.nextToken();
    333.                         try{
    334.                         File isHere=new File(token);
    335.                         findFile=isHere.exists();
    336.                         if (findFile==true)
    337.                         {
    338.                         ta_FileExists=ta_FileExists+token+"
    339. ";
    340.                         }
    341.                         else {
    342.                         ta_FileDoesNotExist=ta_FileDoesNotExist+token+"
    343. ";
    344.                         }
    345.                        
    346.                         }catch(Exception e){ta.append("Error: Can"t execute command "find"
    347. ");}
    348.                         }
    349.                         ta.append("The file(s) exist(s):
    350. ");
    351.                         ta.append(ta_FileExists);
    352.                         ta.append("The file(s) do(es) not exist(s):
    353. ");
    354.                         ta.append(ta_FileDoesNotExist+"
    355. ");
    356.                         ta.append(currentPath+prompt);
    357.                         //ta.setEditable(false);
    358.                 }
    359.                
    360.                 ////////////// BLACK VIDEO ///////////
    361.                 else if (commandEnter.equalsIgnoreCase("bv")) {
    362.                 ta.setBackground(Color.black);
    363.                 tf.setBackground(Color.black);
    364.                 ta.setForeground(Color.lightGray);
    365.                 tf.setForeground(Color.lightGray);
    366.                 ta.setEditable(true);
    367.                 ta.append(commandEnter+"
    368. ");
    369.                 ta.append(currentPath+prompt);
    370.                 //ta.setEditable(false);
    371.                 }
    372.                 ////////////// NORMAL VIDEO ///////////
    373.                 else if (commandEnter.equalsIgnoreCase("nv")) {
    374.                 ta.setBackground(Color.white);
    375.                 tf.setBackground(Color.white);
    376.                 ta.setForeground(Color.black);
    377.                 tf.setForeground(Color.black);
    378.                 ta.setEditable(true);
    379.                 ta.append(commandEnter+"
    380. ");
    381.                 ta.append(currentPath+prompt);
    382.                 //ta.setEditable(false);
    383.                 }
    384.                
    385.                 ////////////// ORANGE VIDEO ///////////
    386.                 else if (commandEnter.equalsIgnoreCase("ov")) {
    387.                 ta.setBackground(Color.black);
    388.                 tf.setBackground(Color.black);
    389.                 ta.setForeground(Color.orange);
    390.                 tf.setForeground(Color.orange);
    391.                 ta.setEditable(true);
    392.                 ta.append(commandEnter+"
    393. ");
    394.                 ta.append(currentPath+prompt);
    395.                 //ta.setEditable(false);
    396.                 }
    397.                 ////////////// YELLOW VIDEO ///////////
    398.                 else if (commandEnter.equalsIgnoreCase("yv")) {
    399.                 ta.setBackground(Color.black);
    400.                 tf.setBackground(Color.black);
    401.                 ta.setForeground(Color.yellow);
    402.                 tf.setForeground(Color.yellow);
    403.                 ta.setEditable(true);
    404.                 ta.append(commandEnter+"
    405. ");
    406.                 ta.append(currentPath+prompt);
    407.                 //ta.setEditable(false);
    408.                 }
    409.                 ////////////// GREEN VIDEO ///////////
    410.                 else if (commandEnter.equalsIgnoreCase("gv")) {
    411.                 ta.setBackground(Color.black);
    412.                 tf.setBackground(Color.black);
    413.                 ta.setForeground(Color.green);
    414.                 tf.setForeground(Color.green);
    415.                 ta.setEditable(true);
    416.                 ta.append(commandEnter+"
    417. ");
    418.                 ta.append(currentPath+prompt);
    419.                 //ta.setEditable(false);
    420.                 }
    421.                 ////////////// DATE ////////////////////
    422.                 else if (commandEnter.equalsIgnoreCase("date")) {
    423.                         Date date=new Date();
    424.                         ta.setEditable(true);
    425.                         ta.append(commandEnter+"
    426. ");
    427.                         ta.append("Current date: "+date+"
    428. ");
    429.                         ta.append(currentPath+prompt);
    430.                         //ta.setEditable(false);
    431.                         tf.requestFocus();
    432.                 }
    433.                 ////////////// CLS ////////////////////
    434.                 else if (commandEnter.equalsIgnoreCase("cls")) {
    435.                         ta.setText("");
    436.                         ta.setText(currentPath+prompt);
    437.                         tf.requestFocus();
    438.                 }
    439.                
    440.                 ///////////  DIRECTORY ///////////////
    441.                 else if (commandEnter.startsWith("dir ")){
    442.                 int j=0;
    443.                 ta.setEditable(true);
    444.                 ta.append(commandEnter+"
    445. ");
    446.                                
    447.                         try{
    448.                         String token=tokenizer.nextToken();
    449.                         token=tokenizer.nextToken();       
    450.                         File dir=new File(token);
    451.                         Temp=dir.getPath();
    452.                         ta.append("Directory: "+dir.getPath()+"
    453. ");
    454.                         p(dir.exists()?"":"The directory does"nt exist
    455. ");
    456.                         String[] fileNames;
    457.                         fileNames=dir.list();
    458.                        
    459.                         for (j=0;j<=fileNames.length;j++) {
    460.                                 int fileSize=fileNames[j].length();
    461.                                 ta.append(fileNames[j]+" , "+"("+fileSize+" bytes)
    462. ");
    463.                                 }
    464.                        
    465.                         }catch(Exception e){}
    466.                        
    467.                         ta.append("
    468. Total Files: "+j);
    469.                         ta.append("
    470. ");
    471.                         ta.append(currentPath+prompt);
    472.                         //ta.setEditable(false);
    473.                 }
    474.                
    475.                 ////////////////////////////////////////////////
    476.                 else if (commandEnter.equalsIgnoreCase("dir")){
    477.                 ta.setEditable(true);
    478.                 ta.append(commandEnter+"
    479. ");
    480.                
    481.                 int j=0;
    482.                         try{
    483.                         File dir=new File(drive_Name+currentPath);
    484.                         ta.append("
    485. Files
    486. ------
    487. ");
    488.                        
    489.                         String[] fileNames;
    490.                         fileNames=dir.list();
    491.                        
    492.                         for (j=0;j<=fileNames.length;j++) {
    493.                                         //File f=new File(drive_Name+currentPath+fileNames[j]);
    494.                                 //System.out.println(currentPath);
    495.                                 File f=new File(fileNames[j]);
    496.                                         //if (f.isDirectory()) {
    497.                                         //ta.append(fileNames[j]+" , (directory)
    498. ");
    499.                                         //int fileSize=fileNames[j].length();
    500.                                         //long fileSize=f.length();
    501.                                         //long modTime=f.lastModified();
    502.                                         //ta.append(fileNames[j]+" , "+"("+fileSize+" bytes)
    503. ");
    504.                                         ta.append(fileNames[j]+"
    505. ");
    506.                                         //if (fileSize==0)ta.append(fileNames[j]+" , "+"(dir)
    507. ");
    508.                                         //}
    509.                                         //else {
    510.                                         //        ta.append(fileNames[j]+" , (file)
    511. ");
    512.                                         //}
    513.                                 }
    514.                        
    515.                         }catch(Exception e){}
    516.                        
    517.                         ta.append("
    518. Total Files: "+j);
    519.                         ta.append("
    520. ");
    521.                         ta.append(currentPath+prompt);
    522.                         //ta.setEditable(false);
    523.                 }
    524.        
    525.                 ////////////// CHANGE DIRECTORY //////////////
    526.                 else if (commandEnter.startsWith("cd ")) {
    527.                                                
    528.                         String ta_List="",currentDir="";
    529.                         ta.setEditable(true);
    530.                         ta.append(commandEnter+"
    531. ");
    532.                        
    533.                         try{
    534.                         String token=tokenizer.nextToken();
    535.                         token=tokenizer.nextToken();
    536.                        
    537.                         File cd=new File(drive_Name+currentPath+"/"+token);
    538.                         path=drive_Name+currentPath+"/"+token;
    539.                         if (cd.exists()==true)currentPath=path;
    540.                         if (cd.exists()==false) ta.append("Error: File does"nt exist
    541. ");
    542.                        
    543.                         }catch(Exception e){ta.append("Error: Can"t execute command "cd"
    544. "); }
    545.                        
    546.                         ta.append("
    547. ");
    548.                         ta.append(currentPath+prompt);
    549.                         //ta.setEditable(false);
    550.                         tf.requestFocus();
    551.                 }               
    552.                 ////////////////////////////////////////////////
    553.                 else if (commandEnter.equalsIgnoreCase("cd"))
    554.          ta.append(commandEnter+"
    555. "+System.getProperty("user.dir")+"
    556. "+currentPath+prompt);
    557.                
    558.                 ////////////////////////////////////////////////
    559.                 else if (commandEnter.equalsIgnoreCase("cd..")){
    560.                 String ta_List="",currentDir="",rootDir="";
    561.                         ta.setEditable(true);
    562.                         ta.append(commandEnter+"
    563. ");
    564.                         try {
    565.                         File dir=new File(currentPath);
    566.                         if (dir.exists()==false)ta.append("Error: Can"t change directory
    567. ");
    568.                         currentPath=dir.getParent();
    569.                         rootDir=dir.getPath();
    570.                         if (currentPath==null) {currentPath=rootDir;}
    571.                                
    572.                         }catch(Exception e){ta.append("Error: Can"t execute command "cd"
    573. ");}
    574.                         ta.append("
    575. ");
    576.                         ta.append(currentPath+prompt);
    577.                         //ta.setEditable(false);
    578.                         tf.requestFocus();
    579.                 }               
    580.                 //////////////////// SHOW FILE //////////////////
    581.                 else if (commandEnter.startsWith("show ") || commandEnter.startsWith("show"))
    582.                 {
    583.                 int ic;
    584.                
    585.                 ta.setEditable(true);
    586.                 ta.append(commandEnter+"
    587. ");
    588.                 String token=tokenizer.nextToken();
    589.                 String fileContent="";
    590.                
    591.                         while(tokenizer.hasMoreTokens()){
    592.                         token=tokenizer.nextToken();       
    593.                        
    594.                         try{
    595.                         FileInputStream fin =new FileInputStream(drive_Name+currentPath+"/"+token);
    596.                        
    597.                         do {
    598.                                 ic=fin.read();
    599.                                 if (ic!=-1) fileContent=fileContent+(char)ic;
    600.                          } while(ic!=-1);
    601.                         fin.close();
    602.                         ta.append(fileContent);
    603.                         fileContent="";
    604.                
    605.                         }catch(Exception e){
    606.                         String cannotShowFile="";
    607.                         cannotShowFile=token;
    608.                         ta.append("Error: Can"t show file ""+cannotShowFile+""
    609. ");
    610.                         }
    611.                         }
    612.                        
    613.                         ta.append("
    614. ");
    615.                         ta.append(currentPath+prompt);
    616.                         //ta.setEditable(false);
    617.                        
    618.                
    619.                 }
    620.                        
    621.                 //////////////////// COPY FILE //////////////////
    622.                 else if (commandEnter.startsWith("copy ") || commandEnter.startsWith("copy"))
    623.                 {
    624.                 int ic;
    625.                
    626.                 ta.setEditable(true);
    627.                 ta.append(commandEnter+"
    628. ");
    629.                
    630.                 String fileContent="";
    631.                 FileInputStream fin;
    632.                 FileOutputStream fout;       
    633.                        
    634.                         try{
    635.                         String token=tokenizer.nextToken();
    636.                         token=tokenizer.nextToken();
    637.                         fin=new FileInputStream(drive_Name+currentPath+"/"+token);
    638.                                                
    639.                         token=tokenizer.nextToken();
    640.                         fout=new FileOutputStream(drive_Name+currentPath+"/"+token);
    641.                        
    642.                         do {
    643.                                 ic=fin.read();
    644.                                 if (ic!=-1) fout.write(ic);
    645.                          } while(ic!=-1);
    646.                         fin.close();
    647.                         fout.close();
    648.                        
    649.                        
    650.                         }catch(Exception e){
    651.                        
    652.                         ta.append("Error: Can"t execute "copy" command
    653. ");
    654.                         }
    655.                         ta.append("
    656. ");
    657.                         ta.append(currentPath+prompt);
    658.                         //ta.setEditable(false);
    659.                        
    660.                 }
    661.                 //////////////////// RENAME FILE //////////////////
    662.                 else if (commandEnter.startsWith("ren ") || commandEnter.startsWith("ren"))
    663.                 {
    664.                                
    665.                 ta.setEditable(true);
    666.                 ta.append(commandEnter+"
    667. ");
    668.                
    669.                         try{
    670.                         String token=tokenizer.nextToken();
    671.                         token=tokenizer.nextToken();
    672.                         File oldFile=new File(drive_Name+currentPath+"/"+token);
    673.                         if (oldFile.exists()==false) ta.append("Error: File ""+oldFile+"" does"nt exist
    674. ");
    675.                                                
    676.                         token=tokenizer.nextToken();
    677.                         File newFile=new File(drive_Name+currentPath+"/"+token);
    678.                         oldFile.renameTo(newFile);
    679.                        
    680.                         }catch(Exception e){
    681.                        
    682.                         ta.append("Error: Can"t execute "ren" command
    683. ");
    684.                         }
    685.                         ta.append("
    686. ");
    687.                         ta.append(currentPath+prompt);
    688.                         //ta.setEditable(false);
    689.                        
    690.                 }
    691.                 //////////////////// DELETE FILE //////////////////
    692.                 else if (commandEnter.startsWith("kill ") || commandEnter.startsWith("kill"))
    693.                 {
    694.                                
    695.                 ta.setEditable(true);
    696.                 ta.append(commandEnter+"
    697. ");
    698.                 String token=tokenizer.nextToken();
    699.                         while(tokenizer.hasMoreTokens()){
    700.                        
    701.                         try{
    702.                         token=tokenizer.nextToken();
    703.                         File killFile=new File(drive_Name+currentPath+"/"+token);
    704.                        
    705.                         if (killFile.exists()==false) ta.append("Error: File ""+killFile+"" does"nt exist
    706. ");
    707.                                                
    708.                         killFile.delete();       
    709.                         }catch(Exception e){                       
    710.                         ta.append("Error: Can"t execute "del" command
    711. ");
    712.                         }
    713.                         }
    714.                         ta.append("
    715. ");
    716.                         ta.append(currentPath+prompt);
    717.                         //ta.setEditable(false);
    718.                        
    719.                 }
    720.                 //////////////////// MAKE DIRECTORY //////////////
    721.                 else if (commandEnter.startsWith("md ") || commandEnter.startsWith("md"))
    722.                 {
    723.                                
    724.                 ta.setEditable(true);
    725.                 ta.append(commandEnter+"
    726. ");
    727.                
    728.                         while(tokenizer.hasMoreTokens()){
    729.                         try{
    730.                         String token=tokenizer.nextToken();
    731.                         token=tokenizer.nextToken();
    732.                         File md=new File(drive_Name+currentPath+"/"+token);
    733.                         md.mkdir();               
    734.                         }catch(Exception e){
    735.                                 ta.append("Error: Can"t execute "md" command
    736. ");
    737.                         }
    738.                         }
    739.                         ta.append("
    740. ");
    741.                         ta.append(currentPath+prompt);
    742.                         //ta.setEditable(false);
    743.                        
    744.                 }
    745.                 //////////////////// EDIT //////////////
    746.                 else if (commandEnter.startsWith("edit ") || commandEnter.startsWith("edit"))
    747.                 {
    748.                                
    749.                         ta.setEditable(true);
    750.                         ta.append(commandEnter+"
    751. ");
    752.                         currentContent=ta.getText();
    753.                         int ic;
    754.                         String fileContent="",currentFile="";
    755.                                                                
    756.                         String token=tokenizer.nextToken();
    757.                         ta.setText("");
    758.                         try{
    759.                        
    760.                         token=tokenizer.nextToken();
    761.                         currentFile=token;                       
    762.                        
    763.                         FileInputStream fin =new FileInputStream(drive_Name+currentPath+"/"+token);
    764.                        
    765.                         do {
    766.                                 ic=fin.read();
    767.                                 if (ic!=-1) fileContent=fileContent+(char)ic;
    768.                          } while(ic!=-1);
    769.                         fin.close();
    770.                         ta.append(fileContent);
    771.                
    772.                         }catch(Exception e){}
    773.                        
    774.                         //ta.setText("");
    775.                         editOn=true;
    776.                         myFrame.setTitle("NetDemo Console - Edit Mode (Type "exit" to quit) - "+currentFile);
    777.                         ta.requestFocus();
    778.                                                
    779.                 }
    780.                 //////////////////// LOCAL DATE  /////////////////
    781.                 else if (commandEnter.startsWith("local ") || commandEnter.startsWith("local"))
    782.                 {
    783.                                
    784.                 ta.setEditable(true);
    785.                 ta.append(commandEnter+"
    786. ");
    787.                
    788.                 String token=tokenizer.nextToken();
    789.                         Date date=new Date();
    790.                         DateFormat df;
    791.                         Locale country=Locale.US;
    792.                
    793.                         try{
    794.                         token=tokenizer.nextToken();
    795.                        
    796.                         if (token.equals("-us")) {       
    797.                         ta.append("United States:
    798. ");
    799.                         country=Locale.US;                       
    800.                         }
    801.                         if (token.equals("-uk")) {       
    802.                         ta.append("United Kingdom:
    803. ");
    804.                         country=Locale.UK;                       
    805.                         }
    806.                         if (token.equals("-jp")) {       
    807.                         ta.append("Japan:
    808. ");
    809.                         country=Locale.JAPAN;                       
    810.                         }
    811.                         if (token.equals("-kr")) {       
    812.                         ta.append("Korea:
    813. ");
    814.                         country=Locale.KOREA;                       
    815.                         }
    816.                         if (token.equals("-fr")) {       
    817.                         ta.append("France:
    818. ");
    819.                         country=Locale.FRANCE;                       
    820.                         }
    821.                         if (token.equals("-gr")) {       
    822.                         ta.append("Germany:
    823. ");
    824.                         country=Locale.GERMANY;                       
    825.                         }
    826.                         if (token.equals("-it")) {       
    827.                         ta.append("Italy:
    828. ");
    829.                         country=Locale.ITALY;                       
    830.                         }
    831.                         if (token.equals("-ca")) {       
    832.                         ta.append("Canada:
    833. ");
    834.                         country=Locale.CANADA;                       
    835.                         }
    836.                         if (token.equals("-ch")) {       
    837.                         ta.append("China:
    838. ");
    839.                         country=Locale.CHINA;                       
    840.                         }
    841.                         if (token.equals("-tw")) {       
    842.                         ta.append("Taiwan:
    843. ");
    844.                         country=Locale.CHINA;                       
    845.                         }
    846.                        
    847.                         df=DateFormat.getTimeInstance(DateFormat.FULL, country);
    848.                         ta.append("Time: "+df.format(date)+"
    849. ");
    850.                         df=DateFormat.getDateInstance(DateFormat.FULL, country);
    851.                         ta.append("Date: "+df.format(date)+"
    852. ");
    853.                        
    854.                         }catch(Exception e){
    855.                                 ta.append("Usage: local -us (United States)
    856. ");
    857.                                 ta.append("       local -uk (United Kingdom)
    858. ");
    859.                                 ta.append("       local -gr (Germany)
    860. ");
    861.                                 ta.append("       local -fr (France)
    862. ");
    863.                                 ta.append("       local -ca (Canada)
    864. ");
    865.                                 ta.append("       local -it (Italy)
    866. ");
    867.                                 ta.append("       local -ch (China)
    868. ");
    869.                                 ta.append("       local -kr (Korea)
    870. ");
    871.                                 ta.append("       local -jp (Japan)
    872. ");
    873.                                
    874.                         }
    875.                
    876.                         ta.append("
    877. ");
    878.                         ta.append(currentPath+prompt);
    879.                         //ta.setEditable(false);
    880.                        
    881.                 }
    882.                 ////////////// SORT /////////////////////
    883.                 else if (commandEnter.startsWith("sort ") || commandEnter.startsWith("sort"))
    884.                 {
    885.                 ta.setEditable(true);
    886.                 ta.append(commandEnter+"
    887. ");
    888.                
    889.                 String token=tokenizer.nextToken();
    890.                 TreeSet ts=new TreeSet();       
    891.                 while(tokenizer.hasMoreTokens()){
    892.                         token=tokenizer.nextToken();
    893.                                                
    894.                         try{
    895.                         ts.add(token);
    896.                         }catch(Exception e){
    897.                         ta.append("Error: Can"t execute command "sort"
    898. ");
    899.                         }
    900.                        
    901.                         }
    902.                         ta.append("The sorted list:
    903. ");
    904.                         ta.append(ts+"
    905. ");       
    906.                         ta.append(currentPath+prompt);
    907.                         //ta.setEditable(false);
    908.                 }
    909.                
    910.                 ////////////// CHECK MEMORY ////////////
    911.                 else if (commandEnter.equalsIgnoreCase("memcheck")) {
    912.                         Runtime r=Runtime.getRuntime();
    913.                         long mem1;
    914.                         ta.setEditable(true);
    915.                         ta.append(commandEnter+"
    916. ");
    917.                         ta.append("Total Memory: "+r.totalMemory()+" bytes"+"
    918. ");
    919.                         ta.append("Initial free Memory: "+r.freeMemory()+" bytes"+"
    920. ");
    921.                         r.gc();
    922.                         mem1=r.freeMemory();
    923.                         ta.append("Free memory after Garbage collection: "+mem1+" bytes
    924. ");
    925.                         ta.append(currentPath+prompt);                               
    926.                         //ta.setEditable(false);
    927.                 }
    928.                
    929.                 ////////////// OS INFORMATION //////////
    930.                 else if (commandEnter.equalsIgnoreCase("osinfo")) {
    931.                         ta.setEditable(true);
    932.                         ta.append(commandEnter+"
    933. ");
    934.                         String os_arch="",os_name="",os_version="",usr_Name="",usr_Home="";
    935.                        
    936.                         os_arch=System.getProperty("os.arch");
    937.                         os_name=System.getProperty("os.name");                       
    938.                         os_version=System.getProperty("os.version");
    939.                         usr_Name=System.getProperty("user.name");
    940.                         usr_Home=System.getProperty("user.home");
    941.                         ta.append("OS Architecture: "+os_arch+" type
    942. ");
    943.                         ta.append("OS Name: "+os_name);
    944.                         if (os_name.equals("Windows 95"))ta.append("(Windows 98/ME)
    945. ");
    946.                         ta.append("OS Version: "+os_version+"
    947. ");
    948.                         ta.append("OS Registered to: "+usr_Name+"
    949. ");
    950.                         ta.append("OS Installation: "+usr_Home+"
    951. ");
    952.                         ta.append(currentPath+prompt);                               
    953.                         //ta.setEditable(false);
    954.                 }
    955.                 ////////////// QUIT ////////////////////
    956.                 else if (commandEnter.equalsIgnoreCase("quit")) {
    957.                         System.exit(0);
    958.                 }
    959.        
    960.                 //////////////   ////////////////////
    961.                 else if (commandEnter.equals("")) {
    962.                         ta.append("
    963. ");       
    964.                         ta.append(currentPath+prompt);
    965.                         tf.requestFocus();
    966.                 }
    967.                 ////////////// PROMPT  ////////////////////
    968.                 else if (commandEnter.startsWith("prompt ") || commandEnter.startsWith("prompt"))
    969.                 {
    970.                 ta.setEditable(true);
    971.                 ta.append(commandEnter+"
    972. ");
    973.                         try {
    974.                         String token=tokenizer.nextToken();
    975.                         token=tokenizer.nextToken();
    976.                         prompt=token;                       
    977.                         } catch(Exception e) {t
    978.             a.append("Error: Parameter missing in "prompt"
    979. Usage: prompt anyword
    980. ");}
    981.                         ta.append(currentPath+prompt);
    982.                         //ta.setEditable(false);
    983.                 }
    984.                
    985.                 ////////////// HELP ////////////////////
    986.                 else if (commandEnter.equalsIgnoreCase("help") || commandEnter.equalsIgnoreCase("?")) {
    987.                         ta.setEditable(true);
    988.                         ta.append(commandEnter+"
    989. ");
    990.                         ta.append("----------|---------------------------|-----------------------------
    991. ");
    992.                         ta.append(" COMMAND  | MEANING                   | EXAMPLE
    993. ");
    994.                         ta.append("----------|---------------------------|-----------------------------
    995. ");
    996.                         ta.append(" ip       | Internet Protocol         | ip yahoo.com hotmail.com
    997. ");
    998.                         ta.append(" run      | Run Program               | run calc notepad word prog
    999. ");
    1000.                         ta.append(" date     | Shows Date                | date
    1001. ");
    1002.                         ta.append(" cls      | Clears Screen             | cls
    1003. ");
    1004.                         ta.append(" cd       | Changes directory         | cd Programs
    1005. ");
    1006.                         ta.append(" copy     | Copy files                | copy readme.txt newreadme.txt
    1007. ");
    1008.                         ta.append(" dir      | Shows Directory           | dir
    1009. ");
    1010.                         ta.append(" memcheck | Checks Memory             | memcheck
    1011. ");
    1012.                         ta.append(" prompt   | Changes prompt sign       | prompt >>>
    1013. ");
    1014.                         ta.append(" urlinfo  | Gives URL information     | urlinfo http://www.yahoo.com
    1015. ");
    1016.                         ta.append(" sort     | Sorts given data          | sort bad perfect great
    1017. ");
    1018.                         ta.append(" edit     | Goes to Edit mode         | edit filename(optional)
    1019. ");
    1020.                         ta.append(" find     | Finds file                | find prog.dbm hello.txt
    1021. ");
    1022.                         ta.append(" kill     | Deletes file              | kill file1.txt file2.txt
    1023. ");
    1024.                         ta.append(" save     | Saves file in edit mode   | save file1 file2 file3
    1025. ");
    1026.                         ta.append(" local    | Shows country Time & Date | local
    1027. ");
    1028.                         ta.append(" nv       | Normal Video              | nv
    1029. ");
    1030.                         ta.append(" bv       | Black Video               | bv
    1031. ");
    1032.                         ta.append(" gv       | Green Video               | gv
    1033. ");
    1034.                         ta.append(" ov       | Orange Video              | ov
    1035. ");
    1036.                         ta.append(" yv       | Yellow Video              | yv
    1037. ");
    1038.                         ta.append(" ren      | Rename files              | ren oldName.txt newName.txt
    1039. ");
    1040.                         ta.append(" md       | Creates new Directory     | md newDirectory
    1041. ");
    1042.                         ta.append(" show     | Shows file content        | show readme.txt
    1043. ");
    1044.                         ta.append(" osinfo   | OS Information            | osinfo
    1045. ");
    1046.                         ta.append(" exit     | Exits from Edit Mode      | exit
    1047. ");
    1048.                         ta.append(" help     | Shows help information    | help
    1049. ");
    1050.                         ta.append(" ?        | Shows help information    | ?
    1051. ");
    1052.                         ta.append(" whois    | Gives information of site | whois internic.net dotcom.com
    1053. ");
    1054.                         ta.append(" quit     | Quits NetDemo / Edit mode | quit
    1055. ");
    1056.                        
    1057.                         ta.append(currentPath+prompt);
    1058.                         tf.requestFocus();
    1059.                 }
    1060.                 ////////////// ERROR  ////////////////////
    1061.                 else
    1062.                 {
    1063.                 ta.setEditable(true);
    1064.                 ta.append(commandEnter+"
    1065. ");
    1066.                 String temp=currentPath+"/"+commandEnter;
    1067.                 String temp2=temp.replace(""," ");
    1068.                 StringTokenizer st=new StringTokenizer(temp2);
    1069.                 String temp3=st.nextToken();
    1070.                 String temp4=st.nextToken();
    1071.                 temp2=temp3+temp4;
    1072.                 //System.out.println(temp2);
    1073.                 try {
    1074.                 Runtime r=Runtime.getRuntime();
    1075.                 Process p=null;
    1076.                 p=r.exec(temp2);
    1077.                
    1078.                 }catch(Exception e){ta.append("Error: Illegal command call
    1079. ");}
    1080.                 ta.append(currentPath+prompt);
    1081.                 //ta.setEditable(false);       
    1082.                 tf.requestFocus();
    1083.                 }
    1084.                
    1085.         }
    1086. }
    1087. public static void main(String args[]) throws Exception{
    1088. myFrame=new Frame("Molecule (NetDemo Console) - Type "help" or "?" for Help");
    1089. NetDemo nd=new NetDemo();
    1090. myFrame.add(nd);
    1091. myFrame.setSize(600,400);
    1092. myFrame.setLocation(200,100);
    1093. myFrame.pack();
    1094. myFrame.setResizable(false);
    1095. myFrame.show();
    1096. myFrame.addWindowListener(new WindowAdapter() {
    1097.         public void windowClosing(WindowEvent we)
    1098.         {System.exit(0);}
    1099.         });
    1100. }
    1101. }
    复制代码

       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/10/2/082125547.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-26 21:53 , Processed in 0.436909 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表