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/
| 驅動器 | 可存取路徑 | 說明 |
|---|---|---|
| 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) {} }
}
try-finally 關閉 Stream 與 FileConnection,防止檔案鎖定/ 結尾,否則 list() 返回空SecurityException (權限不足)、ConnectionNotFoundException (路徑無效)readFully() 大檔案,採用分塊緩衝讀寫掌握上述模式,即可在 Java ME 應用中可靠實現資料持久化、快取管理、日誌記錄等檔案操作需求。