1. 程式人生 > >巧用File的renameto方法實現檔案的批量重新命名以及檔案移動

巧用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); } } }

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