Saving Data-Saving Files 发表于 2015-07-13 | 分类于 Android | 阅读次数: 微言App中有个阅读量的功能,是直接将用户的阅读量保存在SD卡上,我封装成了工具类。 需要权限1<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ReadUtil.java12345678910111213141516171819202122232425262728293031323334353637383940414243444546 public class ReadUtil { public static final File SDCardDir = Environment.getExternalStorageDirectory(); //获取SDCard目录 public static final String SD_ROOT_NAME = "WeiYan/"; public static final String SD_FILE_NAME = "reading"; public static void saveToFile(int readTotal) { try { // 判断SD卡是否存在,并且是否具有读写权限 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File saveFile = new File(SDCardDir + File.separator + SD_ROOT_NAME, SD_FILE_NAME); if (!saveFile.getParentFile().exists()) { saveFile.getParentFile().mkdirs(); } int nowReadingTotal = getReadedTotal() + readTotal; String readedTotal = nowReadingTotal + ""; FileOutputStream outStream = new FileOutputStream(saveFile); outStream.write(readedTotal.getBytes()); outStream.close(); } else { } } catch (Exception e) { e.printStackTrace(); } } public static int getReadedTotal() { int readedTotal = 0; File file = new File(SDCardDir + File.separator + SD_ROOT_NAME, SD_FILE_NAME); if (file.exists() && file.isFile()) { StringBuffer stringBuffer = new StringBuffer(); try { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); int c; while ((c = bufferedReader.read()) != -1) { stringBuffer.append((char) c); } bufferedReader.close(); } catch (Exception e) { e.printStackTrace(); } String result = stringBuffer.toString(); readedTotal = Integer.parseInt(result); } return readedTotal; }} 如何调用读取数据1ReadUtil.getReadedTotal() 保存阅读量1ReadUtil.saveToFile(readTotal); 联系作者 我的微信公众号:吴小龙同学,欢迎关注交流,公号回复关键字「1024」有惊喜哦。