现在的位置: 首页 > 综合 > 正文

blackberry操作sdcard中的文件(创建文件夹/读文件/写文件)

2014年03月03日 ⁄ 综合 ⁄ 共 2753字 ⁄ 字号 评论关闭

创建文件夹:

/**
	 * 创建用户资源文件夹
	 * 
	 * @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;
	}

 

 

 

抱歉!评论已关闭.