一 读取文件
在实际开发过程中有各种各样的需求,不知道你们有没有遇到过类似的需求,几个Fragment切换, 数据是读取SD卡中的json文件或者xml文件,那每次点击Fragment就要读取一次文件,虽然java读取文件很快,不过处理不当也需要200ms如果使用String来存储,这里我使用StringBuilder来存储读出来的内容 , 极大的增加了读取速度:
public String readTxtFile(String strFilePath) {
String path = strFilePath;
StringBuilder builder = new StringBuilder();
//打开文件
File file = new File(path);
//如果path是传递过来的参数,可以做一个非目录的判断
if (file.isDirectory()) {
Log.d("TestFile", "The File doesn't not exist.");
} else {
try {
InputStream instream = new FileInputStream(file);
if (instream != null) {
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
//分行读取
while ((line = buffreader.readLine()) != null) {
builder.appen

本文介绍了Android开发中如何高效读取SD卡上指定后缀的文件,如JSON或XML,以及如何删除目录及其中的文件。此外,还提供了递归查找所有.gif文件的方法,并讨论了集合的正序与倒序排序技巧。
最低0.47元/天 解锁文章
715

被折叠的 条评论
为什么被折叠?



