Java語言實現(xiàn)簡單FTP軟件 FTP本地文件管理模塊實現(xiàn)(9)
本文為大家分享了FTP本地文件管理模塊的實現(xiàn)方法,供大家參考,具體內(nèi)容如下
首先看一下界面:
1、本地文件列表的顯示功能
將本地的當(dāng)前目錄下所有文件顯示出來,并顯示文件的屬性包括文件名、大小、日期、通過javax.swing.JTable()來顯示具體的數(shù)據(jù)。更改當(dāng)前文件目錄會調(diào)用com.oyp.ftp.panel.local.LocalPanel類的listLocalFiles()方法,其主要代碼如下
/** * 讀取本地文件到表格的方法 */ private void listLocalFiles(File selDisk) { if (selDisk == null || selDisk.isFile()) { return; } localSelFilePathLabel.setText(selDisk.getAbsolutePath()); File[] listFiles = selDisk.listFiles(); // 獲取磁盤文件列表 // 獲取表格的數(shù)據(jù)模型 DefaultTableModel model = (DefaultTableModel) localDiskTable.getModel(); model.setRowCount(0); // 清除模型的內(nèi)容 model.addRow(new Object[] { ".", "<DIR>", "" }); // 創(chuàng)建.選項 model.addRow(new Object[] { "..", "<DIR>", "" }); // 創(chuàng)建..選項 if (listFiles == null) { JOptionPane.showMessageDialog(this, "該磁盤無法訪問"); return; } // 遍歷磁盤根文件夾的內(nèi)容,添加到表格中 for (File file : listFiles) { File diskFile = new DiskFile(file); // 創(chuàng)建文件對象 String length = file.length() + "B "; // 獲取文件大小 if (file.length() > 1000 * 1000 * 1000) { // 計算文件G單位 length = file.length() / 1000000000 + "G "; } if (file.length() > 1000 * 1000) { // 計算文件M單位 length = file.length() / 1000000 + "M "; } if (file.length() > 1000) { length = file.length() / 1000 + "K "; // 計算文件K單位 } if (file.isDirectory()) { // 顯示文件夾標志 length = "<DIR>"; } // 獲取文件的最后修改日期 String modifDate = new Date(file.lastModified()).toLocaleString(); if (!file.canRead()) { length = "未知"; modifDate = "未知"; } // 將單個文件的信息添加到表格的數(shù)據(jù)模型中 model.addRow(new Object[] { diskFile, length, modifDate }); } localDiskTable.clearSelection(); // 取消表格的選擇項 }
2、本地文件列表的刷新功能
點擊“刷新”按鈕,會觸發(fā)com.oyp.ftp.panel.local.RefreshAction類的actionPerformed(ActionEvent e)方法,其主要代碼如下
/** * 刷新本地資源列表的動作處理器的事件處理方法 */ @Override public void actionPerformed(ActionEvent e) { this.localPanel.refreshCurrentFolder(); // 調(diào)用管理面板的刷新方法 }
上面的響應(yīng)事件會調(diào)用com.oyp.ftp.panel.local.LocalPanel類的refreshCurrentFolder()方法,其具體代碼如下
/** * 刷新指定文件夾的方法 */ void refreshFolder(File file) { listLocalFiles(file); } /** * 刷新本地當(dāng)前文件夾的方法 */ public void refreshCurrentFolder() { final File file = getCurrentFolder(); // 獲取當(dāng)前文件夾 Runnable runnable = new Runnable() { // 創(chuàng)建新的線程 public void run() { listLocalFiles(file); // 重載當(dāng)前文件夾的列表到表格中 } }; //導(dǎo)致 runnable 的 run 方法在 EventQueue 的指派線程上被調(diào)用。 SwingUtilities.invokeLater(runnable); // 在事件線程中調(diào)用該線程對象 }
3、 新建本地文件夾功能
點擊“新建文件夾”按鈕,會觸發(fā)com.oyp.ftp.panel.local.CreateFolderAction類的actionPerformed(ActionEvent e)方法,然后彈出一個對話框,填寫要新建的文件夾名稱,選擇“確定”,“取消”按鈕結(jié)束。其主要代碼如下
/** * 創(chuàng)建文件夾按鈕的動作處理器的動作事件的方法 */ @Override public void actionPerformed(ActionEvent e) { // 使用輸入對話框接收用戶輸入的文件夾名稱 String folderName = JOptionPane.showInputDialog("請輸入文件夾名稱:"); if (folderName == null) return; File curFolder = null; // 獲取本地資源表格的當(dāng)前選擇行號 int selRow = localPanel.localDiskTable.getSelectedRow(); if (selRow < 0) { // 創(chuàng)建當(dāng)前文件夾對象 curFolder = new File(localPanel.localSelFilePathLabel.getText()); } else { // 獲取表格選擇行的第一個單元值 Object value = localPanel.localDiskTable.getValueAt(selRow, 0); if (value instanceof File) { // 如果單元值是文件,則獲取文件的上級文件夾 curFolder = (File) value; if (curFolder.getParentFile() != null) curFolder = curFolder.getParentFile(); } else // 否則根據(jù)界面的路徑標簽創(chuàng)建當(dāng)前文件夾對象 curFolder = new File(localPanel.localSelFilePathLabel.getText()); } // 創(chuàng)建當(dāng)前文件夾下的新文件夾對象 File tempFile = new File(curFolder, folderName); if (tempFile.exists()) {// 如果存在相同文件或文件夾 JOptionPane.showMessageDialog(localPanel, folderName + "創(chuàng)建失敗,已經(jīng)存在此名稱的文件夾或文件。", "創(chuàng)建文件夾", JOptionPane.ERROR_MESSAGE);// 提示用戶名稱已存在 return; // 結(jié)束本方法 } if (tempFile.mkdir()) // 創(chuàng)建文件夾 JOptionPane.showMessageDialog(localPanel, folderName + "文件夾,創(chuàng)建成功。", "創(chuàng)建文件夾", JOptionPane.INFORMATION_MESSAGE); else JOptionPane.showMessageDialog(localPanel, folderName + "文件夾無法被創(chuàng)建。", "創(chuàng)建文件夾", JOptionPane.ERROR_MESSAGE); this.localPanel.refreshFolder(curFolder);// 刷新文件夾 }
4、刪除本地文件功能
選擇好要刪除的文件或文件夾,點擊“刪除”按鈕,會觸發(fā)com.oyp.ftp.panel.local.DelFileAction類的actionPerformed(ActionEvent e)方法,然后彈出一個對話框,選擇“是”,“否”,“取消”按鈕結(jié)束。其主要代碼如下
/** * 刪除本地文件的動作處理器的處理動作事件的方法 */ public void actionPerformed(ActionEvent e) { // 獲取表格選擇的所有行 final int[] selRows = this.localPanel.localDiskTable.getSelectedRows(); if (selRows.length < 1) // 如果沒有選擇表格內(nèi)容 return; // 結(jié)束該方法 int confirmDialog = JOptionPane.showConfirmDialog(localPanel, "確定要執(zhí)行刪除嗎?"); // 用戶確認是否刪除 if (confirmDialog == JOptionPane.YES_OPTION) { // 如果用于同意刪除 Runnable runnable = new Runnable() { // 創(chuàng)建線程 /** * 刪除文件的遞歸方法 * * @param file * 要刪除的文件對象 */ private void delFile(File file) { try { if (file.isFile()) { // 如果刪除的是文件 boolean delete = file.delete(); // 調(diào)用刪該文件的方法 if (!delete) { JOptionPane.showMessageDialog(localPanel, file .getAbsoluteFile() + "文件無法刪除。", "刪除文件", JOptionPane.ERROR_MESSAGE); return; } } else if (file.isDirectory()) { // 如果刪除的是文件夾 File[] listFiles = file.listFiles();// 獲取該文件夾的文件列表 if (listFiles.length > 0) { for (File subFile : listFiles) { delFile(subFile); // 調(diào)用遞歸方法刪除該列表的所有文件或文件夾 } } boolean delete = file.delete();// 最后刪除該文件夾 if (!delete) { // 如果成功刪除 JOptionPane.showMessageDialog(localPanel, file .getAbsoluteFile() + "文件夾無法刪除。", "刪除文件", JOptionPane.ERROR_MESSAGE); return; // 返回方法的調(diào)用處 } } } catch (Exception ex) { Logger.getLogger(LocalPanel.class.getName()).log( Level.SEVERE, null, ex); } } /** * 線程的主體方法 * * @see java.lang.Runnable#run() */ public void run() { File parent = null; // 遍歷表格的選擇內(nèi)容 for (int i = 0; i < selRows.length; i++) { // 獲取每個選擇行的第一列單元內(nèi)容 Object value = DelFileAction.this.localPanel.localDiskTable .getValueAt(selRows[i], 0); // 如果該內(nèi)容不是DiskFile類的實例對象 if (!(value instanceof DiskFile)) continue; // 結(jié)束本次循環(huán) DiskFile file = (DiskFile) value; if (parent == null) parent = file.getParentFile(); // 獲取選擇文件的上級文件夾 if (file != null) { delFile(file); // 調(diào)用遞歸方法刪除選擇內(nèi)容 } } // 調(diào)用refreshFolder方法刷新當(dāng)前文件夾 DelFileAction.this.localPanel.refreshFolder(parent); JOptionPane.showMessageDialog(localPanel, "刪除成功。"); } }; new Thread(runnable).start(); // 創(chuàng)建并啟動這個線程 } }
5、重命名本地文件功能
選擇好要重命名的文件或文件夾,點擊“重命名”按鈕,會觸發(fā)com.oyp.ftp.panel.local.RennameAction類的actionPerformed(ActionEvent e)方法,其主要代碼如下
/** * 重命名動作的事件處理方法 */ @Override public void actionPerformed(ActionEvent e) { // 獲取本地資源表格的選擇行號 int selRow = this.localPanel.localDiskTable.getSelectedRow(); if (selRow < 0) return; // 獲取選擇行的第一個單元值 Object value = this.localPanel.localDiskTable.getValueAt(selRow, 0); if (!(value instanceof File)) return; // 將該單元值轉(zhuǎn)換為File類的對象 File file = (File) value; // 使用對話框接收用戶如入的新文件名 String fileName = JOptionPane .showInputDialog("請輸入新文件名", file.getName()); if (fileName == null) return; // 創(chuàng)建新名稱的文件 File renFile = new File(file.getParentFile(), fileName); boolean isRename = file.renameTo(renFile); // 將原文件重命名 // 刷新文件夾 this.localPanel.refreshFolder(file.getParentFile()); if (isRename) { JOptionPane.showMessageDialog(this.localPanel, "重命名為" + fileName + "成功。"); } else { JOptionPane.showMessageDialog(this.localPanel, "無法重命名為" + fileName + "。", "文件重命名", JOptionPane.ERROR_MESSAGE); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java多態(tài)的向上轉(zhuǎn)型的概念及實例分析
在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于java多態(tài)的向上轉(zhuǎn)型的概念及實例分析,對此有興趣的朋友們可以跟著學(xué)習(xí)下。2021-05-05SpringBoot之如何搭建SpringBoot+Maven項目
這篇文章主要介紹了SpringBoot之如何搭建SpringBoot+Maven項目問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07Java獲取時間如何將當(dāng)前時間減一天、一月、一年、并格式化
這篇文章主要介紹了Java獲取時間,將當(dāng)前時間減一天、一月、一年,并加以格式化,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09關(guān)于Spring的@Transaction導(dǎo)致數(shù)據(jù)庫回滾全部生效問題(又刪庫跑路)
使用@Transactional一鍵開啟聲明式事務(wù), 這就真的事務(wù)生效了?過于信任框架總有“意外驚喜”。本文通過案例給大家詳解關(guān)于Spring的@Transaction導(dǎo)致數(shù)據(jù)庫回滾全部生效問題,感興趣的朋友一起看看吧2021-05-05Java 使用openoffice進行word轉(zhuǎn)換為pdf的方法步驟
這篇文章主要介紹了Java 使用openoffice進行word轉(zhuǎn)換為pdf的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04