blackberry操作sdcard中的文件(創建文件夾/讀文件/寫文件)

創建文件夾:

/**
	 * 創建用戶資源文件夾
	 * 
	 * @param fileRootUri
	 * @param userQpin
	 * @return
	 * @throws Exception
	 */
	public String createUserResourceFolder(String userQpin) {
		String openUri = "";
		if (!isExistSDCard) {
			openUri = STORE_URI;
		} else {
			openUri = SDCARD_URI;
		}
		FileConnection fc = null;
		try {
			fc = (FileConnection) Connector.open(openUri);
			if (!fc.exists()) {
				fc.mkdir();
			}
			fc.close();
			fc = (FileConnection) Connector.open(openUri + userQpin + "/");
			if (!fc.exists()) {
				fc.mkdir();
			}
			return fc.getURL();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fc != null) {
				try {
					fc.close();
				} catch (IOException e) {
				}
				fc = null;
			}
		}
		return null;

	}


寫文件

/**
	 * 保存文件至指定的文件夾下
	 * 
	 * @param folder
	 *            [] 具體參數請參考 example:String[] IMAGE_FOLDER = { "/image/", ".jpg"
	 *            }
	 * @param String
	 *            返回文件存的路徑
	 * @return
	 */
	public String writeFile(String userQpin, String fileName, String[] folder,
			byte[] data) {
		FileConnection fc = null;
		String openUri = null;
		OutputStream outStream = null;
		if (!isExistSDCard) {
			openUri = STORE_URI;
		} else {
			openUri = SDCARD_URI;
		}
		openUri = openUri + userQpin;
		String fileFolderUri = openUri + folder[0];
		String filePathUri = openUri + folder[0] + fileName + folder[1];
		try {
			fc = (FileConnection) Connector.open(fileFolderUri);
			if (!fc.exists()) {
				fc.mkdir(); // create the file if it doesn't exist
			}
			fc.close();
			fc = (FileConnection) Connector.open(filePathUri);
			if (!fc.exists()) {
				fc.create();
			}
			if (data != null && data.length > 0) {
				outStream = fc.openOutputStream(fc.fileSize());
				outStream.write(data);
				outStream.flush();
			}
			return fc.getPath() + fc.getName();
		} catch (IOException e) {
			e.printStackTrace();
			LogUtil.DEBUG(LogUtil.UNIT_TEST, "save error");
		} finally {
			try {
				if (fc != null) {
					fc.close();
				}
				if (outStream != null) {
					outStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}


讀文件

注意不要使用in.avaliable()獲得文件長度,很可能出現返回爲0的情況。

/**
	 * 讀取文件
	 * @param userQpin
	 * @param fileName
	 * @param folder
	 * @return
	 */
	public byte[] readFile(String filePathUri) {
		
		FileConnection fc = null;
		DataInputStream in = null;
		try {
			fc = (FileConnection) Connector.open(filePathUri);
			in = fc.openDataInputStream();
			int length = (int)fc.fileSize();
			byte[] data = new byte[length];
			in.read(data, 0, length);
			return data;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fc != null) {
					fc.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}


刪除文件以及判斷sdcard是否掛載

/**
	 * 刪除文件
	 * 
	 * @param filePathUri
	 * @return
	 */
	public boolean deleteFile(String filePathUri) {
		FileConnection fc = null;
		try {
			fc = (FileConnection) Connector.open(filePathUri);
			fc.delete();
			return true;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fc != null) {
					fc.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	/**
	 * 檢查當前手機是否掛載SDcard
	 * 
	 * @return
	 */
	public boolean isExistSDCard() {
		Enumeration euu = null;
		try {
			euu = FileSystemRegistry.listRoots();
			while (euu.hasMoreElements()) {
				String str = (String) euu.nextElement();
				if (str.endsWith("/")) {
					str = str.substring(0, str.length() - 1);
				}
				if (SDCARD_STRING.equals(str)) {
					return true;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}


 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章