<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    中文JAVA技術(shù)平等自由協(xié)作創(chuàng)造

    Java專題文章博客和開(kāi)源

    常用鏈接

    統(tǒng)計(jì)

    最新評(píng)論

    Java實(shí)現(xiàn)快速批量移動(dòng)文件

      Java實(shí)現(xiàn)快速批量移動(dòng)文件
      文件移動(dòng)是計(jì)算機(jī)資源管理常用的一個(gè)操作,這在操作系統(tǒng)中可以通過(guò)文件的剪切與復(fù)制或鼠標(biāo)拖動(dòng)來(lái)實(shí)現(xiàn)。但是在Java文件的編程實(shí)現(xiàn)中,大多是通過(guò)復(fù)制文件到目的地,再刪除所有文件來(lái)實(shí)現(xiàn)的。這對(duì)于小文件來(lái)說(shuō)看不出什么弊端,但是如果移動(dòng)幾個(gè)大的文件,則會(huì)使操作緩慢并且浪費(fèi)系統(tǒng)資源。本實(shí)例將通過(guò)File類的renameTo()方法直接實(shí)現(xiàn)文件的快速移動(dòng),哪怕是移動(dòng)幾GB的文件也不會(huì)需要等待太長(zhǎng)時(shí)間。
      思路分析:
      首先是視圖層。在這里有個(gè)建議,因?yàn)樵谀承┛丶氖录校3?huì)訪問(wèn)其他控件,且控件的事件方法用到的參數(shù)幾乎就是固定的ActionEvent類,很少傳遞別的參數(shù)。因此即使視圖是用拖動(dòng)控件自動(dòng)生成的,也要在代碼中把這些控件設(shè)為類的成員變量。在本實(shí)例中,要用到JPanel控件作為其他控件的容器,JLabel控件用來(lái)顯示固定信息,JTextField控件用來(lái)顯示要移動(dòng)的文件和目標(biāo)文件夾,JButton控件用來(lái)選擇源文件夾、目標(biāo)文件夾以及實(shí)現(xiàn)移動(dòng)和關(guān)閉程序,JScrollPane用來(lái)顯示條形柱,以及JTextArea控件用來(lái)顯示操作記錄托福答案
      然后是模型層。對(duì)于瀏覽按鈕,要獲取源文件夾中的文件名數(shù)組和目標(biāo)文件夾的路徑,這就需要定義一個(gè)File型數(shù)組成員變量保存文件名,再定義一個(gè)File型成員變量保存目標(biāo)路徑。
      選擇源文件的瀏覽按鈕后,首先創(chuàng)建一個(gè)JFileChooser文件選擇器,使用JFileChooser類的setMultiSelectionEnabled(true);方法設(shè)置可以多選,通過(guò)JFileChooser類的showOpenDialog(this);方法顯示文件選擇對(duì)話框,若用戶確認(rèn)則使用JFileChooser類的getSelectedFiles()方法獲取選中的文件數(shù)組并賦值給之前定義的File型數(shù)組成員變量,通過(guò)JTextField()類的setText("")方法清空文本框以除去上一次操作的記錄,新建一個(gè)StringBuilder對(duì)象,使用foreach()循環(huán)遍歷文件數(shù)組,通過(guò)StringBuilder類的append()方法連接文件名稱,因?yàn)樽钋懊娑嗔藗€(gè)“、”,再使用StringBuilder類的substring()方法獲取所有文件名稱的字符串,通過(guò)JTextFieldl類的setText()方法將文件名稱字符串顯示到文本框。
      對(duì)于選擇目標(biāo)文件夾的瀏覽按鈕,首先創(chuàng)建一個(gè)JFileChooser文件選擇器,使用JFileChooser類的setFileSelectionMode()方法設(shè)置選擇器只對(duì)文件夾生效,通過(guò)JFileChooser類的showOpenDialog()方法顯示文件打開(kāi)對(duì)話框,使用JFileChooser類的getSelectedFile()方法獲取選擇的文件夾,最后用JTextField控件的setText()方法顯示文件夾到文本框。
      對(duì)于移動(dòng)按鈕的事件處理方法,首先使用數(shù)組的length屬性判斷文件數(shù)組有無(wú)元素,若有則使用foreach()循環(huán)遍歷文件數(shù)組,對(duì)數(shù)組中的每一個(gè)文件元素創(chuàng)建移動(dòng)目標(biāo)文件,通過(guò)JTextArea控件的append()方法顯示移動(dòng)記錄,使用File類的renameTo()方法實(shí)現(xiàn)文件移動(dòng),最后使用JTextArea控件的append()方法顯示移動(dòng)完成信息。
      對(duì)于關(guān)閉按鈕的事件處理方法,使用System類的exit()方法退出程序。
      代碼如下:
      import java.awt.EventQueue;
      public class QuickMoveFiles extends JFrame {
      /**
      *
      */
      private static final long serialVersionUID = -666045931923008374L;
      private JPanel contentPane;
      private JTextArea infoArea;
      private JTextField sourceFolderField;
      private JTextField targetFolderField;
      private File[] files;
      private File dir;
      /**
      * Launch the application.
      */
      public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {
      public void run() {
      try {
      QuickMoveFiles frame = new QuickMoveFiles();
      frame.setVisible(true);
      } catch (Exception e) {
      e.printStackTrace();
      }
      }
      });
      }
      /**
      * Create the frame.
      */
      public QuickMoveFiles() {
      setTitle("\u5FEB\u901F\u6279\u91CF\u79FB\u52A8\u6587\u4EF6");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setBounds(100, 100, 507, 299);
      contentPane = new JPanel();
      contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
      setContentPane(contentPane);
      GridBagLayout gbl_contentPane = new GridBagLayout();
      gbl_contentPane.columnWidths = new int[] { 0, 178, 0, 0, 0, 0 };
      gbl_contentPane.rowHeights = new int[] { 0, 0, 169, 0, 0 };
      gbl_contentPane.columnWeights = new double[] { 0.0, 1.0, 0.0, 0.0, 0.0,
      Double.MIN_VALUE };
      gbl_contentPane.rowWeights = new double[] { 0.0, 0.0, 1.0, 0.0,
      Double.MIN_VALUE };
      contentPane.setLayout(gbl_contentPane);
      JLabel label = new JLabel("\u9009\u62E9\u6E90\u6587\u4EF6\uFF1A");
      GridBagConstraints gbc_label = new GridBagConstraints();
      gbc_label.anchor = GridBagConstraints.EAST;
      gbc_label.insets = new Insets(0, 0, 5, 5);
      gbc_label.gridx = 0;
      gbc_label.gridy = 0;
      contentPane.add(label, gbc_label);
      sourceFolderField = new JTextField();
      GridBagConstraints gbc_sourceFolderField = new GridBagConstraints();
      gbc_sourceFolderField.gridwidth = 3;
      gbc_sourceFolderField.insets = new Insets(0, 0, 5, 5);
      gbc_sourceFolderField.fill = GridBagConstraints.HORIZONTAL;
      gbc_sourceFolderField.gridx = 1;
      gbc_sourceFolderField.gridy = 0;
      contentPane.add(sourceFolderField, gbc_sourceFolderField);
      sourceFolderField.setColumns(10);
      JButton browserButton1 = new JButton("\u6D4F\u89C8");
      browserButton1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      do_browserButton1_actionPerformed(e);
      }
      });
      GridBagConstraints gbc_browserButton1 = new GridBagConstraints();
      gbc_browserButton1.insets = new Insets(0, 0, 5, 0);
      gbc_browserButton1.gridx = 4;
      gbc_browserButton1.gridy = 0;
      contentPane.add(browserButton1, gbc_browserButton1);
      JLabel label_1 = new JLabel(
      "\u9009\u62E9\u76EE\u6807\u6587\u4EF6\u5939\uFF1A");
      GridBagConstraints gbc_label_1 = new GridBagConstraints();
      gbc_label_1.anchor = GridBagConstraints.EAST;
      gbc_label_1.insets = new Insets(0, 0, 5, 5);
      gbc_label_1.gridx = 0;
      gbc_label_1.gridy = 1;
      contentPane.add(label_1, gbc_label_1);
      targetFolderField = new JTextField();
      GridBagConstraints gbc_targetFolderField = new GridBagConstraints();
      gbc_targetFolderField.gridwidth = 3;
      gbc_targetFolderField.insets = new Insets(0, 0, 5, 5);
      gbc_targetFolderField.fill = GridBagConstraints.HORIZONTAL;
      gbc_targetFolderField.gridx = 1;
      gbc_targetFolderField.gridy = 1;
      contentPane.add(targetFolderField, gbc_targetFolderField);
      targetFolderField.setColumns(10);
      JButton browserButton2 = new JButton("\u6D4F\u89C8");
      browserButton2.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      do_browserButton2_actionPerformed(e);
      }
      });
      GridBagConstraints gbc_browserButton2 = new GridBagConstraints();
      gbc_browserButton2.insets = new Insets(0, 0, 5, 0);
      gbc_browserButton2.gridx = 4;
      gbc_browserButton2.gridy = 1;
      contentPane.add(browserButton2, gbc_browserButton2);
      JLabel label_2 = new JLabel("\u64CD\u4F5C\u8BB0\u5F55\uFF1A");
      GridBagConstraints gbc_label_2 = new GridBagConstraints();
      gbc_label_2.anchor = GridBagConstraints.EAST;
      gbc_label_2.insets = new Insets(0, 0, 5, 5);
      gbc_label_2.gridx = 0;
      gbc_label_2.gridy = 2;
      contentPane.add(label_2, gbc_label_2);
      JScrollPane scrollPane = new JScrollPane();
      GridBagConstraints gbc_scrollPane = new GridBagConstraints();
      gbc_scrollPane.gridwidth = 4;
      gbc_scrollPane.insets = new Insets(0, 0, 5, 0);
      gbc_scrollPane.fill = GridBagConstraints.BOTH;
      gbc_scrollPane.gridx = 1;
      gbc_scrollPane.gridy = 2;
      contentPane.add(scrollPane, gbc_scrollPane);
      infoArea = new JTextArea();
      scrollPane.setViewportView(infoArea);
      JButton moveButton = new JButton("\u79FB\u52A8");
      moveButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      do_moveButton_actionPerformed(e);
      }
      });
      GridBagConstraints gbc_moveButton = new GridBagConstraints();
      gbc_moveButton.insets = new Insets(0, 0, 0, 5);
      gbc_moveButton.gridx = 1;
      gbc_moveButton.gridy = 3;
      contentPane.add(moveButton, gbc_moveButton);
      JButton closeButton = new JButton("\u5173\u95ED");
      closeButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      do_closeButton_actionPerformed(e);
      }
      });
      GridBagConstraints gbc_closeButton = new GridBagConstraints();
      gbc_closeButton.insets = new Insets(0, 0, 0, 5);
      gbc_closeButton.gridx = 2;
      gbc_closeButton.gridy = 3;
      contentPane.add(closeButton, gbc_closeButton);
      }
      /**
      * 選擇源文件的瀏覽按鈕
      *
      * @param e
      */
      protected void do_browserButton1_actionPerformed(ActionEvent e) {
      JFileChooser chooser = new JFileChooser();// 創(chuàng)建文件選擇器
      chooser.setMultiSelectionEnabled(true);// 設(shè)置文件多選
      int option = chooser.showOpenDialog(this);// 顯示文件打開(kāi)對(duì)話框
      if (option == JFileChooser.APPROVE_OPTION) {
      files = chooser.getSelectedFiles();// 獲取選擇的文件數(shù)組
      sourceFolderField.setText("");// 清空文本框
      StringBuilder filesStr = new StringBuilder();
      for (File file : files) {// 遍歷文件數(shù)組托福答案
      filesStr.append("、" + file.getName());// 連接文件名稱
      }
      String str = filesStr.substring(1);// 獲取所有文件名稱的字符串
      sourceFolderField.setText(str);// 設(shè)置文件名稱信息到文本框
      } else {
      files = new File[0];
      sourceFolderField.setText("");// 清空文本框
      }
      }
      /**
      * 選擇目標(biāo)文件夾的瀏覽按鈕
      *
      * @param e
      */
      protected void do_browserButton2_actionPerformed(ActionEvent e) {
      JFileChooser chooser = new JFileChooser();// 創(chuàng)建文件選擇器
      // 設(shè)置選擇器只針對(duì)文件夾生效
      chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
      int option = chooser.showOpenDialog(this);// 顯示文件打開(kāi)對(duì)話框
      if (option == JFileChooser.APPROVE_OPTION) {
      dir = chooser.getSelectedFile();// 獲取選擇的文件夾
      targetFolderField.setText(dir.toString());// 顯示文件夾到文本框
      } else {
      dir = null;
      targetFolderField.setText("");
      }
      }
      /**
      * 關(guān)閉按鈕的事件處理方法
      *
      * @param e
      */
      protected void do_closeButton_actionPerformed(ActionEvent e) {
      System.exit(0);
      }
      /**
      * 移動(dòng)按鈕的事件處理方法
      *
      * @param e
      */
      protected void do_moveButton_actionPerformed(ActionEvent e) {
      if (files.length <= 0 || dir == null)// 判斷文件數(shù)組有無(wú)元素
      return;
      for (File file : files) {// 遍歷文件數(shù)組托福答案
      File newFile = new File(dir, file.getName());// 創(chuàng)建移動(dòng)目標(biāo)文件
      infoArea.append(file.getName() + "\t移動(dòng)到\t" + dir);// 顯示移動(dòng)記錄
      file.renameTo(newFile);// 文件移動(dòng)
      infoArea.append("------完成\n");// 顯示移動(dòng)完成信息
      }
      // 顯示操作完成
      infoArea.append("##################操作完成###################\n");
      }
      }

    posted on 2014-05-15 11:29 好不容易 閱讀(290) 評(píng)論(0)  編輯  收藏


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    PK10開(kāi)獎(jiǎng) PK10開(kāi)獎(jiǎng)
    主站蜘蛛池模板: 91亚洲导航深夜福利| 久久久久久A亚洲欧洲AV冫| 亚洲高清中文字幕综合网| 免费看一区二区三区四区| 亚洲人成人网站色www| 99在线视频免费观看| 亚洲国产无套无码av电影| 91免费在线视频| 国产亚洲真人做受在线观看| 国产一级一毛免费黄片| 精品国产亚洲一区二区三区| 国产色无码精品视频免费| 亚洲av一综合av一区| 一级特黄aa毛片免费观看| 亚洲网红精品大秀在线观看| 91精品国产免费网站| 亚洲天堂一区二区三区四区| 最近最新的免费中文字幕 | 久久久久亚洲AV成人无码网站| 91在线免费观看| 亚洲视频精品在线观看| 亚洲精品免费网站| 无码色偷偷亚洲国内自拍| 亚洲色欲色欲www在线丝| 久久A级毛片免费观看| 丁香婷婷亚洲六月综合色| 又粗又大又猛又爽免费视频| 成人国产网站v片免费观看| 精品亚洲永久免费精品| 免费A级毛片无码A∨免费| 亚洲av无码专区青青草原| 亚洲综合网站色欲色欲| 精品一区二区三区无码免费视频| 亚洲最大成人网色香蕉| 免费人成视频x8x8入口| 久久国产精品免费观看| 亚洲色精品三区二区一区| 色噜噜AV亚洲色一区二区| 真人做A免费观看| 亚洲αⅴ无码乱码在线观看性色| 不卡精品国产_亚洲人成在线|