⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frame1.java~60~

📁 一个遍历文件系统目录结构的程序并能够将其结构用字符打印出来
💻 JAVA~60~
字号:
package stream;import java.awt.*;import java.awt.event.*;import javax.swing.*;import com.borland.jbcl.layout.*;import java.io.*;/** * Title: * Description: * Copyright:    Copyright (c) 2002 * Company: * @author * @version 1.0 */public class Frame1 extends JFrame {  RandomAccessFile in_and_out=null;  String str;  int c;boolean start=true;  JPanel contentPane;  JMenuBar jMenuBar1 = new JMenuBar();  JMenu jMenuFile = new JMenu();  JMenuItem jMenuFileExit = new JMenuItem();  JMenu jMenuHelp = new JMenu();  JMenuItem jMenuHelpAbout = new JMenuItem();  BorderLayout borderLayout1 = new BorderLayout();  JPanel jPanel1 = new JPanel();  JPanel jPanel2 = new JPanel();  JPanel jPanel3 = new JPanel();  JPanel jPanel4 = new JPanel();  XYLayout xYLayout2 = new XYLayout();  JLabel jLabel1 = new JLabel();  JTextField jTextField1 = new JTextField();  JLabel jLabel2 = new JLabel();  JTextField jTextField2 = new JTextField();  JScrollPane jScrollPane1 = new JScrollPane();  JTextArea jTextArea1 = new JTextArea();  JButton jButton1 = new JButton();  JButton jButton2 = new JButton();  JButton jButton3 = new JButton();  BorderLayout borderLayout2 = new BorderLayout();  BorderLayout borderLayout3 = new BorderLayout();  /**Construct the frame*/  public Frame1() {   //jTextField2.setText("c:\\fieltree.txt");   //jTextField1.setText("c:\\D");    enableEvents(AWTEvent.WINDOW_EVENT_MASK);    try {      jbInit();    }    catch(Exception e) {      e.printStackTrace();    }  }  /**Component initialization*/  private void jbInit() throws Exception  {    //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));    contentPane = (JPanel) this.getContentPane();    contentPane.setLayout(borderLayout1);    this.setSize(new Dimension(400, 300));    this.setTitle("Frame Title");    jMenuFile.setText("File");    jMenuFileExit.setText("Exit");    jMenuFileExit.addActionListener(new ActionListener()  {      public void actionPerformed(ActionEvent e) {        jMenuFileExit_actionPerformed(e);      }    });    jMenuHelp.setText("Help");    jMenuHelpAbout.setText("About");    jMenuHelpAbout.addActionListener(new ActionListener()  {      public void actionPerformed(ActionEvent e) {        jMenuHelpAbout_actionPerformed(e);      }    });    jPanel1.setLayout(borderLayout2);    jPanel2.setLayout(xYLayout2);    jLabel1.setText("请输入要显示的路径");    jPanel2.setBackground(SystemColor.inactiveCaptionBorder);    jPanel2.setBorder(BorderFactory.createLineBorder(Color.black));    jTextField1.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        jTextField1_actionPerformed(e);      }    });    jLabel2.setText("请输入目标文件名");    //jTextField2.setText("c:\\fieltree.txt");    jPanel3.setLayout(borderLayout3);    jButton1.setText("退出");    jButton2.setText("显示");    jButton3.setText("开始");    jButton3.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        jButton3_actionPerformed(e);      }    });    jPanel4.setBorder(BorderFactory.createLineBorder(Color.black));    jTextField2.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        jTextField2_actionPerformed(e);      }    });    jMenuFile.add(jMenuFileExit);    jMenuHelp.add(jMenuHelpAbout);    jMenuBar1.add(jMenuFile);    jMenuBar1.add(jMenuHelp);    this.setJMenuBar(jMenuBar1);    contentPane.add(jPanel1, BorderLayout.CENTER);    jPanel1.add(jPanel2, BorderLayout.NORTH);    jPanel2.add(jTextField1, new XYConstraints(154, 16, 198, -1));    jPanel2.add(jTextField2,   new XYConstraints(155, 42, 197, -1));    jPanel2.add(jLabel1,   new XYConstraints(22, 15, -1, 22));    jPanel2.add(jLabel2,  new XYConstraints(22, 43, 111, 21));    jPanel1.add(jPanel3, BorderLayout.CENTER);    jPanel3.add(jScrollPane1, BorderLayout.CENTER);    jScrollPane1.getViewport().add(jTextArea1, null);    jPanel1.add(jPanel4, BorderLayout.SOUTH);    jPanel4.add(jButton3, null);    jPanel4.add(jButton2, null);    jPanel4.add(jButton1, null);  }  /**File | Exit action performed*/  public void jMenuFileExit_actionPerformed(ActionEvent e) {    System.exit(0);  }  /**Help | About action performed*/  public void jMenuHelpAbout_actionPerformed(ActionEvent e) {    Frame1_AboutBox dlg = new Frame1_AboutBox(this);    Dimension dlgSize = dlg.getPreferredSize();    Dimension frmSize = getSize();    Point loc = getLocation();    dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);    dlg.setModal(true);    dlg.show();  }  /**Overridden so we can exit when window is closed*/  protected void processWindowEvent(WindowEvent e) {    super.processWindowEvent(e);    if (e.getID() == WindowEvent.WINDOW_CLOSING) {      jMenuFileExit_actionPerformed(null);    }  }   //编写定位方法  public void locate(int b)  {String s; char a[]=new char[100];      for(int i=0;i<b;i++)      {a[i]=' ';      }    s=new String(a,0,b-1);     try     {in_and_out.writeBytes(s);     }     catch(IOException e)     {System.out.println("????????");     }  }  //编写一级子目录和文件遍历方法   public void root(File classpath,int d )   {int i=0;    locate(d);    try    {in_and_out.writeBytes(classpath.getAbsolutePath()+"\n");      // in_and_out.writeBytes(classpath.getName());       System.out.println("show the name:"+classpath.getName());     }    catch(IOException e)      {System.out.println("????????");      }    d=d+classpath.toString().length()-2;    c=d;    File file[];    if(classpath.isDirectory())    {file=classpath.listFiles();       for(i=0;i<file.length;i++)       { if(i==file.length-1)         {  locate(c);            if(file[i].isDirectory())            {             try             {in_and_out.writeBytes("\\--");             }            catch(IOException e)            {System.out.println("????????");            }             start=false;            }            else            {            try            {in_and_out.writeBytes("\\--"+file[i].getName()+"\n");             }              catch(IOException e)              {System.out.println("????????");              }              start=false;               continue;            }         }         else         { locate(c);            if(file[i].isDirectory())            {            try            {in_and_out.writeBytes("+--");            }            catch(IOException e)            {System.out.println("????????");            }            }            else            { try              {in_and_out.writeBytes("+--"+file[i].getName()+"\n");               }               catch(IOException e)               {System.out.println("????????");               }               continue;            }     tree(file[i],c+5);    }       }      //    for(int q=0;q<file.length;q++)     {System.out.println(file[q].getName());     }    }   }  //编写二级以上目录和文件的递归遍历方法  public void tree(File a,int b)  { File array[];     if(a.isDirectory())        {array=a.listFiles();          try {in_and_out.writeBytes(a.getName()+"\n");}      catch(IOException e)      {System.out.println("????????");}          int d=b+5;          for(int k=0;k<array.length;k++)             { if(k==array.length-1)                {if(start)                  {locate(c);                   try                   {in_and_out.writeBytes("|");                   }                   catch(IOException e)                   {System.out.println("????????");                   }                   locate(b-c);                   try                   {in_and_out.writeBytes("\\--");                   }                  catch(IOException e)                  {System.out.println("????????");                  }                  }                else                {locate(b);                try                {in_and_out.writeBytes("\\--");                }                catch(IOException e)                {System.out.println("????????");                }                }                }             else             {if(start)               {locate(c);               try               {in_and_out.writeBytes("|");               }               catch(IOException e)               {System.out.println("????????");               }               locate(b-c);               try               {in_and_out.writeBytes("+--");               }               catch(IOException e)               {System.out.println("????????");               }               }             else               {locate(b);              try              {in_and_out.writeBytes("+--");               }               catch(IOException e)               {System.out.println("????????");}               }             }    tree(array[k],d);        }    } else try {in_and_out.writeBytes(a.getName()+"\n");}      catch(IOException e)      {System.out.println("????????");}  }  void jButton3_actionPerformed(ActionEvent e) {  System.out.println("get the output file"+jTextField2.getText());   System.out.println("get the input path"+jTextField1.getText());     File path=new File("d:\\my");   String a=path.getName();     try {System.out.println("get the output file"+str);System.out.println("get the input path"+jTextField1.getText());in_and_out=new RandomAccessFile("wang.txt","rw");      }    catch(FileNotFoundException f)    {System.out.println("PPPP?????");    }  root(path ,5);try{ in_and_out.close();}      catch(IOException d)     {  System.out.println("Can't close");     }  }  void jTextField1_actionPerformed(ActionEvent e)  {  }  void jTextField2_actionPerformed(ActionEvent e)  {  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -