使用Profile檔案記錄JFileChooser(或者File控制項)最新開啟的檔案路徑,
一、描述
我們使用JFileChooser或者File控制項開啟Windows系統目錄下的檔案之後,如何儲存我們最近開啟的檔案路徑,使得每次開啟檔案就能開啟最近一次開啟的檔案目錄,而不是每次預設開啟C:\Users\Administrator\Documents目錄。我們需要使用Profile屬性檔案來記錄最近開啟的檔案路徑,在File控制項開啟檔案前先讀取設定檔中的最近檔案目錄,在開啟檔案後將路徑儲存到Profile檔案中,在操作完畢點擊確定按鈕後寫入Profile設定檔。
二、原始碼
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Properties;import javax.swing.JFileChooser;import javax.swing.JOptionPane;import javax.swing.filechooser.FileNameExtensionFilter;public class CountUserServer{ public static File chooseFile; private static String latestPath ; private static Profile profile; private static JFileChooser fileChooser; public CountUserServer (){ profile = new Profile();//每次運行程式時建立設定檔Profile對象 //讀取設定檔裡的參數Profile並賦值給latestPath,如果設定檔中沒有該記錄則設定一個預設路徑 latestPath = (profile.read()?profile.latestPath:"D:/KKServer/MainServer/"); try{ if(!new File(latestPath).exists()){ latestPath = "D:/KKServer/MainServer/"; //設定預設的最新路徑 } fileChooser = new JFileChooser(latestPath); //過濾.log類型的檔案FileNameExtensionFilter filter = new FileNameExtensionFilter("Text Files", "log");fileChooser.setFileFilter(filter); int returnValue = fileChooser.showOpenDialog(null);if (returnValue == fileChooser.APPROVE_OPTION) { chooseFile = fileChooser.getSelectedFile(); latestPath = chooseFile.getParent();//每次退出檔案選取器後更新目錄Properties profile.write(latestPath); new OnlineCountDialog();} }catch(FileNotFoundException e){ e.printStackTrace(); } } public static void main(String[] args){ new CountUserServer(); }} class Profile{ //設定預設的最新路徑 String latestPath = "D:/KKServer/MainServer/"; //在當前工程目錄下建立setLatestPath.properties設定檔 File file = new File("./setLatestPath.properties"); public Profile(){} boolean create(){ boolean flag = true; if(file!=null){ File directory = file.getParentFile(); //獲得檔案的父目錄 if(!directory.exists()){ //父目錄不存在時 flag = directory.mkdirs(); //建立父目錄 }else{ //存在目錄 if(!file.exists()){//設定檔不存在時 try { flag = file.createNewFile();//建立設定檔 } catch (IOException e) { flag = false; } } } } return flag; } /** * 讀取屬性檔案中最新開啟檔案的目錄 * @return */ public boolean read(){ Properties properties; //聲明屬性集 FileInputStream inputStream = null; //聲明檔案輸入資料流 boolean b = true; //聲明boolean傳回值 if(!file.exists()){ //設定檔不存在時 b = create(); //調用create()方法建立一個設定檔 if(b) { //設定檔建立成功後 b = write(latestPath);//調用write()將latestPath寫入設定檔 }else{ //建立失敗即不存在設定檔時彈出對話方塊提示錯誤 JOptionPane.showConfirmDialog(null, "對不起,不存在設定檔!", "錯誤",JOptionPane.YES_NO_OPTION,JOptionPane.ERROR_MESSAGE); } }else{ try { inputStream = new FileInputStream(file); properties = new Properties(); properties.load(inputStream);//讀取屬性 latestPath = properties.getProperty("latestPath");//讀取配置參數latestPath的值 inputStream.close(); }catch (IOException ex) { ex.printStackTrace(); b = false; } } return b; } /** * 將最新開啟檔案的目錄儲存到屬性檔案中 * @param latestPath * @return */ public boolean write(String latestPath){ this.latestPath = latestPath; Properties properties = null; FileOutputStream outputStream = null; boolean flag = true; try { outputStream = new FileOutputStream(file); properties = new Properties(); properties.setProperty("latestPath",latestPath); properties.store(outputStream,null); //將屬性寫入 outputStream.flush(); outputStream.close(); }catch (IOException ioe) { flag = false; ioe.printStackTrace(); } return flag; }}
三、總結
1、每次開啟File控制項或者JFileChooser時先讀取Profile檔案中的路徑資訊,如果存在就讀取,如果不存在就設定一個預設的路徑;
2、選擇某個檔案後就將新的檔案路徑寫入Profile檔案,以便下次讀取。