巧用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(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        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[0].getParent();

        for (int i = 0; i < files.length; i++) {
            File file3 = new File(s, "c语言开发Demo" + i);
            file = files[i];
            file.renameTo(file3);
        }
    }

}

在这里界面的实现比较简单,只有三个按钮,在重命名中只要点击一个文件夹就行,方式需要注意的是,在文件移动的时候我们需要先找到保存位置,在进行移动,因为源代码被自己写死了,只能这样子,大家可以进行扩展,比如进行文件筛选后在移动,显示文件移动的进度等

猜你喜欢

转载自blog.csdn.net/qq_37657081/article/details/78522900
今日推荐