天天看點

巧用File的renameto方法實作檔案的批量重命名以及檔案移動

直接上代碼

package com.lzw;

import java.awt.BorderLayout;

public class Demo extends JFrame {

    private JPanel contentPane;
    JFileChooser jFileChooser;
    JButton jButton;// 實作檔案的重命名
    JButton jButton2;// 實作檔案的移動
    JButton jButton3;//選擇檔案的移動位置
    String pathString;

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

    /**
     * Create the frame.
     */
    public Demo() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(, , , );
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(, , , ));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout());

        jButton2 = new JButton("移動檔案");
        jButton2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                moveFile();
            }
        });

        JButton btnNewButton = new JButton("重命名檔案");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                initdata();
            }
        });
        jButton3=new JButton("移動到");
        jButton3.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                JFileChooser jFileChooser1=new JFileChooser();
                jFileChooser1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//隻能移動檔案
                jFileChooser1.showOpenDialog(Demo.this);
                   pathString=jFileChooser1.getSelectedFile().toPath().toString();
                   System.out.println("路徑為:"+pathString);
            }
        });
         contentPane.add(jButton2,BorderLayout.NORTH);
        contentPane.add(btnNewButton,BorderLayout.SOUTH);
        contentPane.add(jButton3);
    }

    protected void moveFile() {
       //在這裡我們實作的是選中的檔案移動到f盤,寫死,簡化
        JFileChooser jFileChooser=new JFileChooser();
        jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//隻能移動檔案
        jFileChooser.showOpenDialog(this);
        System.out.println("sgshshs");
            File file=jFileChooser.getSelectedFile();
            //我們應該先移動檔案夾
            File cacheFile=new File(pathString, file.getName());
            file.renameTo(cacheFile);

//          File[] files=file.listFiles();
//            for(int j=0;j<files.length;j++){
//              File file2=files[j];
//                file=new File(pathString,file2.getName()); 
//                file2.renameTo(file);
//                System.out.println(file2.getName()+"正在移動到"+file2.getPath());
//        }
 //這是實作檔案内容的移動,當存在缺點,會留下一個空檔案夾
    }

    private void initdata() {
        // TODO Auto-generated method stub
        jFileChooser = new JFileChooser();
        jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        jFileChooser.showOpenDialog(this);
        jFileChooser.setMultiSelectionEnabled(false);
        File file = jFileChooser.getSelectedFile();
        if (!(file.isDirectory())) {
            JOptionPane.showMessageDialog(null, "你選中的不是檔案夾");
            return;
        }
        File[] files = file.listFiles();
        String s = files[].getParent();

        for (int i = ; i < files.length; i++) {
            File file3 = new File(s, "c語言開發Demo" + i);
            file = files[i];
            file.renameTo(file3);
        }
    }

}
           

在這裡界面的實作比較簡單,隻有三個按鈕,在重命名中隻要點選一個檔案夾就行,方式需要注意的是,在檔案移動的時候我們需要先找到儲存位置,在進行移動,因為源代碼被自己寫死了,隻能這樣子,大家可以進行擴充,比如進行檔案篩選後在移動,顯示檔案移動的進度等