标签:android style blog color io ar for 文件 数据
float cacheSize = dirSize(new File(Environment.getExternalStorageDirectory() + AppConstants.APP_CACHE_FOLDER)) / 1024.0f / 1024.0f; tvCacheSize.setText(((int) (cacheSize * 100)) / 100.0f + "M"); /** * Return the size of a directory in bytes */ private long dirSize(File dir) { if (dir.exists()) { long result = 0; File[] fileList = dir.listFiles(); for (int i = 0; i < fileList.length; i++) { // Recursive call if it‘s a directory if (fileList[i].isDirectory()) { result += dirSize(fileList[i]); } else { // Sum the file size in bytes result += fileList[i].length(); } } return result; // return the file size } return 0; } case R.id.clearCacheLayout: try { DeleteRecursive(new File(Environment.getExternalStorageDirectory() + AppConstants.APP_CACHE_FOLDER)); Toast.makeText(mActivity, "缓存已清除", Toast.LENGTH_SHORT).show(); float cacheSize = dirSize(new File(Environment.getExternalStorageDirectory() + AppConstants.APP_CACHE_FOLDER)) / 1024.0f / 1024.0f; tvCacheSize.setText(((int) (cacheSize * 100)) / 100 + "M"); } catch (Exception e) { e.printStackTrace(); } break; /** * 删除某个文件夹下的所有文件夹和文件 * * @param delpath */ private void DeleteRecursive(File fileOrDirectory) { if (fileOrDirectory.isDirectory()) for (File child : fileOrDirectory.listFiles()) DeleteRecursive(child); fileOrDirectory.delete(); }
读取Assets文件内容
//从assets 文件夹中获取文件并读取数据 public String getFromAssets(String fileName){ String result = ""; try { InputStream in = getResources().getAssets().open(fileName); //获取文件的字节数 int lenght = in.available(); //创建byte数组 byte[] buffer = new byte[lenght]; //将文件中的数据读到byte数组中 in.read(buffer); result = EncodingUtils.getString(buffer, ENCODING); } catch (Exception e) { e.printStackTrace(); } return result; } }
标签:android style blog color io ar for 文件 数据
原文地址:http://www.cnblogs.com/niray/p/4007793.html