blackberry操作sdcard中的文件(创建文件夹/读文件/写文件)
2012-08-01 14:55
260 查看
创建文件夹:
写文件
读文件
注意不要使用in.avaliable()获得文件长度,很可能出现返回为0的情况。
删除文件以及判断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; }
相关文章推荐
- 真机调试时 无法在sdcard创建文件夹 或者push文件时没有权限
- C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件....
- android模拟器创建和使用SDCard存储和使用图片、视频文件的操作方法
- PHP----文件操作5-----文件(文件夹)的创建和删除
- Android SDcard目录文件操作
- Android 文件在SDCard上读写操作
- 文件及文件夹操作(打开关闭创建流)
- 读取Assets所存在的所有文件(遍历每一个文件夹),并存入sdcard里面
- 该文件没有程序与之关联来执行该操作。请在控制面板的文件夹选项中创建关联 解决代码
- Android SDCard操作(文件读写,容量计算)
- android操作sdcard中的多媒体文件(二)——音乐列表的更新
- 【转】Android SDCard操作(文件读写,容量计算)
- 打开图片,提示:该文件没有程序与之关联来执行该操作。请在控制面板的文件夹选项中创建关联
- Android在SDCard创建目录_卸载应用同时删除工程创建的文件
- Android中创建文件夹和文件的操作
- net8:简易的文件磁盘管理操作一(包括文件以及文件夹的编辑创建删除移动拷贝重命名等)
- C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件转http://www.cnblogs.com/zhuzhiyuan/archive/2011/04/22/2024485.html
- Android SdCard 新建文件夹并在文件夹中读、写文件
- android操作sdcard中的多媒体文件(一)——音乐列表的制作
- PHP操作文件类(文件和文件夹创建,复制,移动和删除)