這幾天初次開始學習運用手機的文件系統,在網上也搜索了一些資料,通過不斷地嘗試和運用,對手機的文件系統也瞭解不少。
1、手機的文件系統
手機的文件系統就像電腦的硬盤一樣,供用戶存儲文件夾和文件。在wtk2.52的模擬器下,默認的根目錄爲root1/,底下有一個子文件夾photos/。在Nokia 的機器上,手機存儲的根目錄爲C:/,存儲卡的目錄爲E:/。操作手機的文件系統條件:手機要支持JSR75,對於未簽名的MIDlet程序還要在程序管理中設置相應的權限才能操作文件系統。
2、JSR75
如果你沒有JSR75的api,到這裏可以下載。
JSR75包括FileSystem部分和PIM部分,現在還沒用到PIM部分,暫時不考慮。下面簡單的介紹些FileSystem所用到的一些類和接口:
- ConnectionClosedException —— 當試圖對一個已經關閉的 FileConnection 對象進行操作時,會拋出此異常。
- FileConnection —— 這是一個很關鍵的類,基本上文件讀寫都是有該類完成。可以從字面上去理解它的意思。
- FileSystemEventHandler —— 這個類我也沒用過,不知道它具體的功能。
- FileSystemListener —— 用於監聽文件系統目錄狀態的變化,比如文件的刪除、新增,存儲卡的拔出和插入等。
- FileSystemRegistry —— 用於管理和跟蹤文件系統的監聽器,以及可以通過此類得到所有文件系統的根目錄。
- IllegalModeException —— 文件打開模式異常,當試圖寫入以只讀方式打開的文件時,會拋出該異常。
3、我個人對文件系統的理解
首先操作FileSystem最常用的類是FileConnection,結合流的操作進行文件的讀和寫。操作文件很像操作網絡連接,一個是FileConnection,一個則是HttpConnection,兩者在操作時都需要一個url(路徑),文件系統是localhost,HttpFileConnection則是請求一個網絡連接。所以我把文件系統的操作也理解爲請求,只不過沒有POST和GET之分。
4.簡單示例
下面的示例裏面提供了列出根目錄,以及目標文件夾下的文件和文件夾的方法,文件和文件夾的區別在於文件名包含”/”字符。
經過我在N95 的機器上測試,C:/,C:/data/我們是無法進行讀寫的,通過應用程序只能夠讀取到C:/data/下的sounds/,images/,videos/這三個文件夾,即使在Nokia真機上的C:/data/下包含別的文件或文件夾,應用程序也無法讀取到。
/** * * @author 水貨程序員 */ public class FileSystem { /** * 判斷是否支持文件系統 * @return */ public boolean isFileSystemSupport() { String version = System.getProperty("microedition.io.file.FileConnection.version"); if (version == null) { return false; } return true; } /** * 獲取所有根目錄 * @return */ private Vector getRoots() { Vector rootVector = new Vector(); Enumeration enums = FileSystemRegistry.listRoots(); while (enums.hasMoreElements()) { String rootName = enums.nextElement().toString(); rootVector.addElement(rootName); } return rootVector; } /** * 獲取目標文件夾下的所有直系子文件夾 * @param path * @return */ public Vector getFolders(String path) { Vector folderVector = new Vector(); try { FileConnection fc = (FileConnection) Connector.open(path, Connector.READ); if (fc.exists()) { boolean isFolder = fc.isDirectory(); if (isFolder) { Enumeration enums = fc.list(); while (enums.hasMoreElements()) { String name = enums.nextElement().toString(); //如果有斜槓,就是文件夾 if (name.indexOf("/") > 0) { folderVector.addElement(name); } } } } fc.close(); } catch (IOException ex) { ex.printStackTrace(); } return folderVector; } /** * 獲取目標文件夾下的所有文件 * @param path * @return */ public Vector getFiles(String path) { Vector fileVector = new Vector(); try { FileConnection fc = (FileConnection) Connector.open(path, Connector.READ); if (fc.exists()) { boolean isFolder = fc.isDirectory(); if (isFolder) { Enumeration enums = fc.list(); while (enums.hasMoreElements()) { String name = enums.nextElement().toString(); //如果沒有斜槓,就是文件 if (name.indexOf("/") < 0) { fileVector.addElement(name); } } } } fc.close(); } catch (IOException ex) { ex.printStackTrace(); } return fileVector; } /** * 獲取目標文件夾下的所有文件和直系子文件夾 * @param path * @return */ public Vector getFoldersAndFiles(String path) { Vector v = new Vector(); try { FileConnection fc = (FileConnection) Connector.open(path, Connector.READ); if (fc.exists()) { boolean isFolder = fc.isDirectory(); if (isFolder) { Enumeration enums = fc.list(); while (enums.hasMoreElements()) { String name = enums.nextElement().toString(); v.addElement(name); } } } fc.close(); } catch (IOException ex) { ex.printStackTrace(); } return v; } }
代碼用法舉例:如果要獲取C:/data/images/底下的文件列表,我們在傳入path時應該這樣寫
FileSystem fs = new FileSystem();
Vector v = fs.getFiles(“files:///c:/data/images/”);