• 开发技巧


    遍历系统目录

    遍历系统文件目录,可以查找到我们需要的文件,方便记录文件的目录地址(不想找database在系统中的位置,就写了这么个玩意儿,一定要开启一个线程,不然手机文件多,可能会崩溃)

    优点是:无论手机中有什么文件,都能够便利出来。缺点是手机文件越大,遍历需要的时间久越长久。

    示例代码如下:

     1     private void printFileList(int level,String fileName){
     2         level++;
     3         File file = new File(fileName);
     4         if(file.isDirectory()){//判断是否是文件夹
     5             if(file.listFiles()!=null){
     6                 for (File temp:file.listFiles()){
     7                     printFileList(level,temp.getAbsolutePath());
     8                 }
     9             }
    10 
    11         }else{//是文件直接打印当前目录
    12             StringBuilder sb = new StringBuilder();
    13             for (int i = 1;i<level;i++){
    14                 sb.append("-");
    15             }
    16             System.out.println(sb.toString()+fileName);
    17         }
    18 
    19     }

    调用代码如下:

    printFileList(0,"/");

     读取已经存在的数据库文件

    在开发应用的时候,为了省事儿,客户端的筒子们可能会得到一个完整的db文件,而不是json串。比如手机号查询系统,点餐系统,医疗系统,都会将服务端生成的db文件直接打包,客户端调用接口的时候返回这个db文件进行操作,这样可以保证本地机器未联网的情况下也可以进行正常的操作。

    示例代码如下:

     (文件只加载了一次,如果db文件需要更新,请缩小if的范围)

     1     /**
     2      * 获取数据库路径,将assets目录中的文件加载到dababase目录下面。同样我们可以直接读取网络下载是数据,然后直接将数据流存放到本地demo.db文件中
     3      * @param context
     4      * @return
     5      */
     6     private String getDataBasePath(Context context) {
     7         String dbName = "demo.db";
     8         String dbDir = mContext.getDatabasePath(dbName).getAbsolutePath().replaceAll(dbName,"");//没有文件名
     9         String dbPath = mContext.getDatabasePath(dbName).getAbsolutePath();//有文件名
    10         File pathFile = new File(dbPath);
    11         File dirFile = new File(dbDir);
    12         if (!pathFile.exists()) {//检查一下 database 目录是否存在
    13             try {
    14                 if (!dirFile.exists()) {//database目录不存在,新建
    15                     dirFile.mkdir();
    16                 }
    17                 if(!pathFile.exists()){//文件不存在就直接创建新的文件
    18                     pathFile.createNewFile();
    19                 }
    20                 // 得到 assets 目录下我们实现准备好的 SQLite 数据库作为输入流
    21                 InputStream is = context.getAssets().open(dbName);
    22                 // 输出流
    23                 OutputStream os = new FileOutputStream(dbPath);
    24                 // 文件写入
    25                 byte[] buffer = new byte[1024];
    26                 int length;
    27                 while ((length = is.read(buffer)) > 0) {
    28                     os.write(buffer, 0, length);//写入到/data/data/packagename/databases/demo.db目录下
    29                 }
    30                 // 关闭文件流
    31                 os.flush();
    32                 os.close();
    33                 is.close();
    34             } catch (Exception e) {
    35                 e.printStackTrace();
    36             }
    37         }
    38 
    39         return dbPath;
    40     }
  • 相关阅读:
    谈谈焦虑
    Bridge
    Abstract Factory
    开篇辞
    缓存穿透和缓存雪崩
    缓存与数据库的一致性思考
    GDB
    代码格式化
    CSMA/CA协议
    18年秋招(19届)复习指导+面经总结,手把手教你进大厂——阿里、网易、京东、多益等
  • 原文地址:https://www.cnblogs.com/chun-jiang-chao-de-gu-shi/p/5460984.html
Copyright © 2020-2023  润新知