android中一些文件的操作方法
2012-10-15 23:46
190 查看
1.创建文件
2.读取图片文件
3.读取文本文件
4.删除文件(目录)
5.计算SD卡的剩余空间
6.向手机写图片
7.将输入流转换为字符串
File Directory=mContext.getDir("mydir", Context.MODE_PRIVATE); File dest = new File(Directory, filename);
2.读取图片文件
public static Drawable loadImageFromFilesystem(Context context, String filename, int originalDensity) { File Directory = context.getDir("mydir", Context.MODE_PRIVATE); File f = new File( Directory , filename ); InputStream is = null; try { is = new FileInputStream(f); } catch (FileNotFoundException e) { e.printStackTrace(); } Bitmap bmp = BitmapFactory.decodeStream(is ); Drawable drawable = new BitmapDrawable(bmp); ; return drawable; }
3.读取文本文件
public static String read( Context context, String fileName ) { try { FileInputStream in = context.openFileInput(fileName); return readInStream(in); } catch (Exception e) { e.printStackTrace(); } return ""; } private static String readInStream(FileInputStream inStream) { try { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[512]; int length = -1; while((length = inStream.read(buffer)) != -1 ) { outStream.write(buffer, 0, length); } outStream.close(); inStream.close(); return outStream.toString(); } catch (IOException e) { Log.i("FileTest", e.getMessage()); } return null; }
4.删除文件(目录)
public static void delFolder(String folderPath) { try { delAllFile(folderPath); // 删除完里面所有内容 String filePath = folderPath; filePath = filePath.toString(); java.io.File myFilePath = new java.io.File(filePath); myFilePath.delete(); // 删除空文件夹 } catch (Exception e) { System.out.println("删除文件夹操作出错"); e.printStackTrace(); } } public static void delAllFile(String path) { File file = new File(path); if (!file.exists()) { return; } if (!file.isDirectory()) { return; } String[] tempList = file.list(); File temp = null; for (int i = 0; i < tempList.length; i++) { if (path.endsWith(File.separator)) { temp = new File(path + tempList[i]); } else { temp = new File(path + File.separator + tempList[i]); } if (temp.isFile()) { temp.delete(); } if (temp.isDirectory()) { delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件 delFolder(path + "/" + tempList[i]);// 再删除空文件夹 } } }
5.计算SD卡的剩余空间
public static long getFreeDiskSpace() { String status = Environment.getExternalStorageState(); long freeSpace = 0; if (status.equals(Environment.MEDIA_MOUNTED)) { try { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); freeSpace = availableBlocks * blockSize / 1024; } catch (Exception e) { e.printStackTrace(); } } else { return -1; } return (freeSpace); }
6.向手机写图片
public static boolean writeFile( byte[] buffer, String folder, String fileName ) { boolean writeSucc = false; boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); String folderPath = ""; if( sdCardExist ) { folderPath = Environment.getExternalStorageDirectory() + File.separator + folder + File.separator; } else { writeSucc =false; } File fileDir = new File(folderPath); if(!fileDir.exists()) { fileDir.mkdirs(); } File file = new File( folderPath + fileName ); FileOutputStream out = null; try { out = new FileOutputStream( file ); out.write(buffer); writeSucc = true; } catch (Exception e) { e.printStackTrace(); } finally { try {out.close();} catch (IOException e) {e.printStackTrace();} } return writeSucc; }
7.将输入流转换为字符串
/** * 将输入流转化为字符串 * @param inputStream * @return */ public String inStream2string(InputStream inputStream) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; try { int len = -1; while ((len = inputStream.read(buf)) != -1) { baos.write(buf, 0, len); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return new String(baos.toByteArray()); }
相关文章推荐
- Android中的多种文件读写操作方法
- android反编译apk后,java文件中出现的一些类似乱码的问题
- android布局文件中一些属性介绍
- Android中的多种文件读写操作方法
- android内部存储外部存储以及assets文件的操作一些操作
- android开发中调用系统自带的文件选择器遇到的一些问题
- [JAVA] Android用到的一些文件操作
- Android中一些配置文件的介绍
- Android布局文件的一些属性值
- android 文件操作方法集合类分享
- Android写出高效清晰Layout布局文件的一些技巧
- Android device monitor文件管理的一些常见问题
- Android中将清单文件的一些配置参数
- Android中一些常见文件格式分析
- Android布局文件layout.xml的一些属性值
- Android布局文件layout.xml的一些属性值
- Android 文件读写操作方法总结
- android创建文件夹和文件的一些经验教训
- android 一些文件的位置和作用
- Android布局文件layout.xml的一些属性值