android中一些文件的操作方法
来源:互联网 发布:c语言多线程教程 编辑:程序博客网 时间:2024/06/03 12:53
1.创建文件
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 blocke.printStackTrace();}return new String(baos.toByteArray());}
- android中一些文件的操作方法
- C#的一些常用文件操作方法[极好]
- C#的一些常用文件操作方法[极好]
- STL中vector元素的一些操作方法
- C#中常用的文件操作方法
- C#中常用的经典文件操作方法
- C#中常用的文件操作方法
- C#中常用的文件操作方法
- C#中常用的经典文件操作方法
- C#中常用的文件操作方法
- C#中常用的文件操作方法
- C#中常用的经典文件操作方法
- C#中常用的文件操作方法
- C#中常用的文件操作方法
- C#中常用的文件操作方法
- C#中常用的经典文件操作方法
- C#中常用的文件操作方法
- C#中常用的文件操作方法
- HDU 1224 Free DIY Tour
- 一见钟情!Java闭包
- 预见未来
- 日记
- Win32与MFC,精华贴!!!值得拥有
- android中一些文件的操作方法
- 【java 面试100】20.输入一个表示整数的字符串,把该字符串转换成整数并输出
- 在这里记录自己学习中遇到的零碎的容易忽略的知识点,不定时总结更新
- BCB中AnsiSting类方法总结
- ios开发答疑录系列--xcode 的target总是“My Mac 64-bit” 而不是 simulator or device
- Access实现limit类似的功能的方法
- php 内存知识
- 深入解析序列点
- 将DBGrid的数据导出到word和excel中