欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實(shí)現(xiàn)文件批量重命名具體實(shí)例

 更新時(shí)間:2014年02月28日 16:36:58   作者:  
這篇文章主要介紹了Java實(shí)現(xiàn)文件批量重命名具體實(shí)例,需要的朋友可以參考下

Windows操作系統(tǒng)可以實(shí)現(xiàn)重命名文件操作,卻不能實(shí)現(xiàn)批量重命名。本實(shí)例實(shí)現(xiàn)了批量重命名功能,可以將一個(gè)文件夾內(nèi)同一類型的文件按照一定的規(guī)則批量重命名。用戶可以給出重命名模板,程序可以根據(jù)模板對(duì)相應(yīng)的文件進(jìn)行重命名。此外,還可以在重命名模板中添加特殊符號(hào),程序會(huì)將這些特殊符號(hào)替換成重命名后的文件編號(hào)。

思路分析:

1.先看視圖層,需要一些JLabel控件分別顯示指示用戶的信息,三個(gè)JTextField控件分別顯示所選路徑、輸入文件名模板即輸入擴(kuò)展名,兩個(gè)JButton控件分別用來(lái)瀏覽文件夾和開(kāi)始重命名,一個(gè)JSeparator控件表示分割線,一個(gè)JSpinner控件代表開(kāi)始編號(hào),一個(gè)JScrollPane控件作為容器,在里面放置一個(gè)JTable控件列出舊文件名和新文件名。
2.再看模型層。首先定義瀏覽按鈕的事件處理方法,在該方法中創(chuàng)建一個(gè)JFileChooser文件選擇器,使用JFileChooser類的setFileSelectionMode()方法設(shè)置只選擇文件夾,通過(guò)JFileChooser類的showOpenDialog()顯示打開(kāi)對(duì)話框,若用戶點(diǎn)擊確認(rèn)按鈕則使用JFileChooser類的getSelectedFile()方法獲取選中的文件夾,最后使用JTextField控件的setText()方法顯示文件夾信息。
3.定義一個(gè)類來(lái)實(shí)現(xiàn)FileFilter接口,在該類的構(gòu)造方法中保存文件擴(kuò)展名,然后定義一個(gè)方法,在該方法中使用String類的endsWith()方法來(lái)過(guò)濾文件擴(kuò)展名。
4.然后定義開(kāi)始按鈕的事件處理方法,首先使用JTextField控件的getText()方法獲取模板字符串,若為空則通過(guò)JOptionPane類的showMessageDialog()方法提示用戶輸入模板,然后創(chuàng)建DefaultTableModel對(duì)象并使用JTable類的getModel()方法獲取表格數(shù)據(jù)模型,用JTable類的setRowCount(0);方法清除表格數(shù)據(jù),使用JSpinner類的getValue()方法獲取起始編號(hào),使用String類的indexOf方法獲取第一個(gè)“#”的索引,使用String類的substring()方法獲取模板中數(shù)字占位字符串,使用String類的replace()方法把模板中數(shù)字占位字符串替換為指定格式,為了規(guī)范使用String類的toLowerCase()方法將用戶輸入的擴(kuò)展名轉(zhuǎn)換為小寫(xiě)形式,若用戶未輸入“.”則補(bǔ)上,然后使用File類的listFiles()方法獲取文件夾中的文件列表數(shù)組,使用foreach()循環(huán)遍歷每個(gè)文件,通過(guò)String類的format()方法格式化每個(gè)文件名稱,使用DefaultTableModel類的addRow()方法把文件的舊名稱和新名稱添加到表格的數(shù)據(jù)模型中,使用File類的getParentFile()方法獲取目標(biāo)文件所在的文件夾對(duì)象,創(chuàng)建一個(gè)File對(duì)象并初始化為新的文件名,最后使用File類的renameTo()方法實(shí)現(xiàn)文件重命名。
代碼如下:

復(fù)制代碼 代碼如下:

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileFilter;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

/**
 * 獲取文件列表的過(guò)濾器
 * 
 * @author 李鐘尉
 */
public class RenameFiles extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 4534371106024773867L;

    private final class ExtNameFileFilter implements FileFilter {
        private String extName;

        public ExtNameFileFilter(String extName) {
            this.extName = extName;// 保存文件擴(kuò)展名
        }

        @Override
        public boolean accept(File pathname) {
            // 過(guò)濾文件擴(kuò)展名
            if (pathname.getName().toUpperCase()
                    .endsWith(extName.toUpperCase()))
                return true;
            return false;
        }
    }

    private JPanel contentPane;
    private JTextField forderField;
    private JTextField templetField;
    private File dir;
    private JTable table;
    private JTextField extNameField;
    private JSpinner startSpinner;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    RenameFiles frame = new RenameFiles();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public RenameFiles() {
        setResizable(false);
        setTitle("文件批量重命名");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 383, 409);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[] { 72, 54, 60, 87, 91, 0 };
        gbl_contentPane.rowHeights = new int[] { 25, 25, 10, 25, 24, 25, 2,
                216, 0 };
        gbl_contentPane.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
                Double.MIN_VALUE };
        gbl_contentPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
                0.0, 0.0, 0.0, Double.MIN_VALUE };
        contentPane.setLayout(gbl_contentPane);

        JLabel label = new JLabel();
        label.setText("文件批量重命名模塊:");
        GridBagConstraints gbc_label = new GridBagConstraints();
        gbc_label.fill = GridBagConstraints.VERTICAL;
        gbc_label.insets = new Insets(0, 0, 5, 5);
        gbc_label.gridwidth = 3;
        gbc_label.gridx = 1;
        gbc_label.gridy = 0;
        contentPane.add(label, gbc_label);

        JLabel label_1 = new JLabel();
        label_1.setText("文件路徑:");
        GridBagConstraints gbc_label_1 = new GridBagConstraints();
        gbc_label_1.anchor = GridBagConstraints.EAST;
        gbc_label_1.fill = GridBagConstraints.VERTICAL;
        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);

        forderField = new JTextField();
        forderField.setText("");
        GridBagConstraints gbc_forderField = new GridBagConstraints();
        gbc_forderField.fill = GridBagConstraints.HORIZONTAL;
        gbc_forderField.insets = new Insets(0, 0, 5, 5);
        gbc_forderField.gridwidth = 3;
        gbc_forderField.gridx = 1;
        gbc_forderField.gridy = 1;
        contentPane.add(forderField, gbc_forderField);

        JButton button = new JButton();
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_button_actionPerformed(e);
            }
        });
        button.setText("瀏覽");
        GridBagConstraints gbc_button = new GridBagConstraints();
        gbc_button.anchor = GridBagConstraints.NORTHWEST;
        gbc_button.insets = new Insets(0, 0, 5, 0);
        gbc_button.gridx = 4;
        gbc_button.gridy = 1;
        contentPane.add(button, gbc_button);

        JSeparator separator_1 = new JSeparator();
        GridBagConstraints gbc_separator_1 = new GridBagConstraints();
        gbc_separator_1.fill = GridBagConstraints.BOTH;
        gbc_separator_1.insets = new Insets(0, 0, 5, 0);
        gbc_separator_1.gridwidth = 5;
        gbc_separator_1.gridx = 0;
        gbc_separator_1.gridy = 2;
        contentPane.add(separator_1, gbc_separator_1);

        JLabel label_5 = new JLabel();
        label_5.setText("使用#可以指定數(shù)字計(jì)數(shù)所占的位置,使用*可以插入原文件名:");
        GridBagConstraints gbc_label_5 = new GridBagConstraints();
        gbc_label_5.fill = GridBagConstraints.VERTICAL;
        gbc_label_5.insets = new Insets(0, 0, 5, 0);
        gbc_label_5.gridwidth = 5;
        gbc_label_5.gridx = 0;
        gbc_label_5.gridy = 3;
        contentPane.add(label_5, gbc_label_5);

        JLabel label_3 = new JLabel();
        label_3.setText("  模板:");
        GridBagConstraints gbc_label_3 = new GridBagConstraints();
        gbc_label_3.anchor = GridBagConstraints.EAST;
        gbc_label_3.fill = GridBagConstraints.VERTICAL;
        gbc_label_3.insets = new Insets(0, 0, 5, 5);
        gbc_label_3.gridx = 0;
        gbc_label_3.gridy = 4;
        contentPane.add(label_3, gbc_label_3);

        templetField = new JTextField();
        templetField.setText("catrestaurant###");
        GridBagConstraints gbc_templetField = new GridBagConstraints();
        gbc_templetField.anchor = GridBagConstraints.SOUTH;
        gbc_templetField.fill = GridBagConstraints.HORIZONTAL;
        gbc_templetField.insets = new Insets(0, 0, 5, 5);
        gbc_templetField.gridwidth = 3;
        gbc_templetField.gridx = 1;
        gbc_templetField.gridy = 4;
        contentPane.add(templetField, gbc_templetField);

        JLabel label_4 = new JLabel();
        label_4.setText("開(kāi)始于:");
        GridBagConstraints gbc_label_4 = new GridBagConstraints();
        gbc_label_4.fill = GridBagConstraints.VERTICAL;
        gbc_label_4.insets = new Insets(0, 0, 5, 5);
        gbc_label_4.gridx = 0;
        gbc_label_4.gridy = 5;
        contentPane.add(label_4, gbc_label_4);

        startSpinner = new JSpinner();
        GridBagConstraints gbc_startSpinner = new GridBagConstraints();
        gbc_startSpinner.fill = GridBagConstraints.HORIZONTAL;
        gbc_startSpinner.insets = new Insets(0, 0, 5, 5);
        gbc_startSpinner.gridx = 1;
        gbc_startSpinner.gridy = 5;
        contentPane.add(startSpinner, gbc_startSpinner);

        JLabel label_2 = new JLabel();
        label_2.setText("  擴(kuò)展名:");
        GridBagConstraints gbc_label_2 = new GridBagConstraints();
        gbc_label_2.fill = GridBagConstraints.HORIZONTAL;
        gbc_label_2.insets = new Insets(0, 0, 5, 5);
        gbc_label_2.gridx = 2;
        gbc_label_2.gridy = 5;
        contentPane.add(label_2, gbc_label_2);

        JButton startButton = new JButton();
        startButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_startButton_actionPerformed(e);
            }
        });

        extNameField = new JTextField();
        extNameField.setText("jpg");
        GridBagConstraints gbc_extNameField = new GridBagConstraints();
        gbc_extNameField.fill = GridBagConstraints.HORIZONTAL;
        gbc_extNameField.insets = new Insets(0, 0, 5, 5);
        gbc_extNameField.gridx = 3;
        gbc_extNameField.gridy = 5;
        contentPane.add(extNameField, gbc_extNameField);
        startButton.setText("開(kāi)始");
        GridBagConstraints gbc_startButton = new GridBagConstraints();
        gbc_startButton.anchor = GridBagConstraints.NORTH;
        gbc_startButton.insets = new Insets(0, 0, 5, 0);
        gbc_startButton.gridx = 4;
        gbc_startButton.gridy = 5;
        contentPane.add(startButton, gbc_startButton);

        JSeparator separator_2 = new JSeparator();
        GridBagConstraints gbc_separator_2 = new GridBagConstraints();
        gbc_separator_2.anchor = GridBagConstraints.NORTH;
        gbc_separator_2.fill = GridBagConstraints.HORIZONTAL;
        gbc_separator_2.insets = new Insets(0, 0, 5, 0);
        gbc_separator_2.gridwidth = 5;
        gbc_separator_2.gridx = 0;
        gbc_separator_2.gridy = 6;
        contentPane.add(separator_2, gbc_separator_2);

        JScrollPane scrollPane = new JScrollPane();
        GridBagConstraints gbc_scrollPane = new GridBagConstraints();
        gbc_scrollPane.fill = GridBagConstraints.BOTH;
        gbc_scrollPane.gridwidth = 5;
        gbc_scrollPane.gridx = 0;
        gbc_scrollPane.gridy = 7;
        contentPane.add(scrollPane, gbc_scrollPane);

        table = new JTable();
        table.setModel(new DefaultTableModel(new Object[][] {}, new String[] {
                "舊文件名", "新文件名" }));
        scrollPane.setViewportView(table);
    }

    /**
     * 瀏覽按鈕的事件處理方法
     * 
     * @param e
     */
    protected void do_button_actionPerformed(ActionEvent e) {
        JFileChooser chooser = new JFileChooser();// 創(chuàng)建文件選擇器
        // 設(shè)置只選擇文件夾
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int option = chooser.showOpenDialog(this);// 顯示打開(kāi)對(duì)話框
        if (option == JFileChooser.APPROVE_OPTION) {
            dir = chooser.getSelectedFile();// 獲取選擇的文件夾
        } else {
            dir = null;
        }
        forderField.setText(dir + "");// 顯示文件夾信息
    }

    /**
     * 開(kāi)始按鈕的事件處理方法
     * 
     * @param e
     */
    protected void do_startButton_actionPerformed(ActionEvent e) {
        String templet = templetField.getText();// 獲取模板字符串
        if (templet.isEmpty()) {
            JOptionPane.showMessageDialog(this, "請(qǐng)確定重命名模板", "信息對(duì)話框",
                    JOptionPane.WARNING_MESSAGE);
            return;
        }
        // 獲取表格數(shù)據(jù)模型
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.setRowCount(0);// 清除表格數(shù)據(jù)
        int bi = (Integer) startSpinner.getValue();// 獲取起始編號(hào)
        int index = templet.indexOf("#");// 獲取第一個(gè)“#”的索引
        String code = templet.substring(index);// 獲取模板中數(shù)字占位字符串
        // 把模板中數(shù)字占位字符串替換為指定格式
        templet = templet.replace(code, "%0" + code.length() + "d");
        String extName = extNameField.getText().toLowerCase();
        if (extName.indexOf(".") == -1)
            extName = "." + extName;
        // 獲取文件中文件列表數(shù)組
        File[] files = dir.listFiles(new ExtNameFileFilter(extName));
        for (File file : files) {// 變量文件數(shù)組
            // 格式化每個(gè)文件名稱
            String name = String.format(templet, bi++) + extName;
            // 把文件的舊名稱與新名稱添加到表格的數(shù)據(jù)模型
            model.addRow(new String[] { file.getName(), name });
            File parentFile = file.getParentFile();// 獲取文件所在文件夾對(duì)象
            File newFile = new File(parentFile, name);
            file.renameTo(newFile);// 文件重命名
        }
    }
}

效果如圖:

相關(guān)文章

  • Java框架入門之簡(jiǎn)單介紹SpringBoot框架

    Java框架入門之簡(jiǎn)單介紹SpringBoot框架

    今天給大家?guī)?lái)的是關(guān)于Java的相關(guān)知識(shí),文章圍繞著SpringBoot框架展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • 詳解Java如何實(shí)現(xiàn)一個(gè)優(yōu)秀的散列表

    詳解Java如何實(shí)現(xiàn)一個(gè)優(yōu)秀的散列表

    這篇文章主要通過(guò)簡(jiǎn)單的示例為大家詳細(xì)介紹了在Java中如何實(shí)現(xiàn)一個(gè)優(yōu)秀的散列表,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,需要的可以了解一下
    2023-07-07
  • Java數(shù)據(jù)結(jié)構(gòu)及算法實(shí)例:漢諾塔問(wèn)題 Hanoi

    Java數(shù)據(jù)結(jié)構(gòu)及算法實(shí)例:漢諾塔問(wèn)題 Hanoi

    這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)及算法實(shí)例:漢諾塔問(wèn)題 Hanoi,本文直接給出實(shí)現(xiàn)代碼,代碼中包含大量注釋,需要的朋友可以參考下
    2015-06-06
  • SpringIOC DI循環(huán)依賴實(shí)例詳解

    SpringIOC DI循環(huán)依賴實(shí)例詳解

    這篇文章主要介紹了SpringIOC——DI循環(huán)依賴,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • springboot如何從數(shù)據(jù)庫(kù)獲取數(shù)據(jù),用echarts顯示(數(shù)據(jù)可視化)

    springboot如何從數(shù)據(jù)庫(kù)獲取數(shù)據(jù),用echarts顯示(數(shù)據(jù)可視化)

    這篇文章主要介紹了springboot如何從數(shù)據(jù)庫(kù)獲取數(shù)據(jù),用echarts顯示(數(shù)據(jù)可視化),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java switch 語(yǔ)句如何使用 String 參數(shù)

    Java switch 語(yǔ)句如何使用 String 參數(shù)

    這篇文章主要介紹了Java switch 語(yǔ)句如何使用 String 參數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下
    2019-06-06
  • Spring事務(wù)控制策略及@Transactional失效問(wèn)題解決避坑

    Spring事務(wù)控制策略及@Transactional失效問(wèn)題解決避坑

    這篇文章主要為大家介紹了Spring事務(wù)控制策略及@Transactional失效問(wèn)題解決避坑,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • Spring bean生命周期配置過(guò)程解析

    Spring bean生命周期配置過(guò)程解析

    這篇文章主要介紹了Spring bean生命周期配置過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • SpringMVC異常處理的三種方式

    SpringMVC異常處理的三種方式

    在SpringMVC中異常處理是一個(gè)重要的方面,它幫助我們有效地處理應(yīng)用程序中的異常情況,提高用戶體驗(yàn)和系統(tǒng)的穩(wěn)定性,這篇文章主要給大家介紹了關(guān)于SpringMVC異常處理的三種方式,需要的朋友可以參考下
    2024-02-02
  • 通過(guò)java生成讀取二維碼詳解

    通過(guò)java生成讀取二維碼詳解

    這篇文章主要介紹了java二維碼生成讀取詳解,二維碼再生活在無(wú)處不在,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,下面和小編一起來(lái)學(xué)習(xí)一下吧
    2019-05-05

最新評(píng)論