首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> J2ME

JSR-75 文件系統操作完整指南:從路徑解析到讀寫最佳實踐

     編輯:J2ME

JSR-75 文件連接 API 核心架構

JSR-75 (FileConnection Optional Package) 為 Java ME 提供標準化本地檔案存取能力。核心入口:javax.microedition.io.Connector.open() 返回 FileConnection 介面,支援檔案/目錄的 CRUD 操作。

環境檢測與路徑規範

// 1. 檢測裝置支援性
public boolean isFileSystemSupported() {
    String version = System.getProperty("microedition.io.file.FileConnection.version");
    return version != null;
}

// 2. 標準檔案 URL 格式
// 本機檔案:file://localhost/:
// 範例:file://localhost/E:/images/photo.jpg
// 目錄結尾必須加 '/':file://localhost/E:/images/

S60v3 典型目錄結構與存取權限

驅動器可存取路徑說明
E: (記憶卡)E:/ 所有目錄完全讀寫權限
C: (手機內存)C:/data/Images/圖片目錄
C:/data/Sounds/音訊目錄
C:/data/Videos/視訊目錄

注意:嘗試存取 C: 其他目錄將拋出 SecurityException。可透過 WTK 內建 PDADemo → FileBrowser 實測裝置實際可見路徑。

核心操作代碼範本

開啟連線 (統一入口)

FileConnection fc = (FileConnection) Connector.open(
    "file://localhost/E:/data.txt", Connector.READ_WRITE);

讀取檔案

public byte[] readFile(String fileUrl) {
    FileConnection fc = null;
    InputStream is = null;
    try {
        fc = (FileConnection) Connector.open(fileUrl, Connector.READ);
        if (!fc.exists()) return null;
        is = fc.openInputStream();
        byte[] data = new byte[is.available()];
        is.read(data);
        return data;
    } catch (IOException e) { return null; }
    finally {
        try { if (is != null) is.close(); } catch (IOException ignored) {}
        try { if (fc != null) fc.close(); } catch (IOException ignored) {}
    }
}

寫入檔案 (覆蓋模式)

public void writeFile(String fileUrl, byte[] data) {
    FileConnection fc = null;
    OutputStream os = null;
    try {
        fc = (FileConnection) Connector.open(fileUrl, Connector.READ_WRITE);
        fc.create(); // 不存在則建立
        os = fc.openOutputStream(); // 預設從開頭寫入,截斷原內容
        os.write(data);
    } catch (IOException e) { /* 處理異常 */ }
    finally {
        try { if (os != null) os.close(); } catch (IOException ignored) {}
        try { if (fc != null) fc.close(); } catch (IOException ignored) {}
    }
}

關鍵陷阱:追加寫入需自行實現

JSR-75 預設不支援追加模式。openOutputStream() 總是從位移 0 開始寫入,且不會自動清空原檔案尾部殘留資料。

// 正確的追加寫入模式
public void appendFile(String fileUrl, byte[] newData) {
    byte[] oldData = readFile(fileUrl); // 先讀取原內容
    if (oldData == null) oldData = new byte[0];
    
    byte[] combined = new byte[oldData.length + newData.length];
    System.arraycopy(oldData, 0, combined, 0, oldData.length);
    System.arraycopy(newData, 0, combined, oldData.length, newData.length);
    
    writeFile(fileUrl, combined); // 完整覆寫
}

目錄列舉

public Vector listDirectory(String dirUrl) {
    FileConnection fc = null;
    try {
        fc = (FileConnection) Connector.open(dirUrl, Connector.READ);
        if (!fc.exists() || !fc.isDirectory()) return null;
        Enumeration en = fc.list(); // 返回檔名/目錄名 (含 '/' 尾碼)
        Vector vec = new Vector();
        while (en.hasMoreElements()) vec.addElement(en.nextElement());
        return vec;
    } catch (IOException e) { return null; }
    finally { try { if (fc != null) fc.close(); } catch (IOException ignored) {} }
}

刪除檔案/目錄

public void deleteFile(String fileUrl) {
    FileConnection fc = null;
    try {
        fc = (FileConnection) Connector.open(fileUrl, Connector.READ_WRITE);
        if (fc.exists()) fc.delete();
    } catch (IOException e) { /* 處理異常 */ }
    finally { try { if (fc != null) fc.close(); } catch (IOException ignored) {} }
}

最佳實踐總結

  1. 資源管理:嚴格使用 try-finally 關閉 Stream 與 FileConnection,防止檔案鎖定
  2. 路徑處理:目錄 URL 必須以 / 結尾,否則 list() 返回空
  3. 異常處理:捕獲 SecurityException (權限不足)、ConnectionNotFoundException (路徑無效)
  4. 大檔案處理:避免一次性 readFully() 大檔案,採用分塊緩衝讀寫
  5. 執行緒安全:FileConnection 非執行緒安全,多執行緒存取需外部同步

掌握上述模式,即可在 Java ME 應用中可靠實現資料持久化、快取管理、日誌記錄等檔案操作需求。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved