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");
}
}