• 每日日报


    使用上下文(Context)获取常见目录

    Context 可以理解成大工具类
    通过上下文 可以访问跟当前应用相关的 全局信息(系统资源) 也可以访问当前应用的私有资源 和类
    也可以做系统级的调用 比如 开启另外一个activity 发广播 开启服务
     
    getFilesDir(); 操作的是 data/data/包名/files
    openFileInput->FileInputStream         这两个方法获取的流实际上操作的就是getFilesDir()对应目录下的文件
    openFileOutput->FileOutputStream
     
    访问/data/data/包名 这个私有目录 一定要使用上下文获取路径
    public static boolean saveInfobycontext(Context context,String username, String pwd) {
    
    String info = username+"##"+pwd;
    
    // File file = new File("data/data/com.itheima.logindemo/info.txt");
    
    //使用上下文获取应用相关私有路径
    
    // File file = new File(context.getFilesDir().getAbsolutePath()+"/info.txt");
    
    try {
    
    // FileOutputStream fos = new FileOutputStream(file);
    
    FileOutputStream fos = context.openFileOutput("info2.txt", Context.MODE_PRIVATE);
    
    fos.write(info.getBytes());
    
    fos.close();
    
    return true;
    
    } catch (Exception e) {
    
    e.printStackTrace();
    
    return false;
    
    }
    
    }
    
    public static String[] readInfobyContext(Context context){
    
    File file = new File("data/data/com.itheima.logindemo/info.txt");
    
    try {
    
    //FileInputStream fis = new FileInputStream(file);
    
    FileInputStream fis = context.openFileInput("info2.txt");
    
    BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
    
    String temp = reader.readLine();
    
    String[] result = temp.split("##");
    
    return result;
    
    } catch (Exception e) {
    
    e.printStackTrace();
    
    return null;
    
    }
    
    }
  • 相关阅读:
    求字符串中最大的递增子序列
    分析函数改写自关联
    收集统计信息让SQL走正确的执行计划
    利用case when 减少表扫描次数
    利用查询提示优化SQL
    利用SQL进行推理
    查找字段连续相同的最大值
    优化有标量子查询的SQL
    将部分相同的多行记录转成一行多列
    .net 测试工具类
  • 原文地址:https://www.cnblogs.com/zhukaile/p/14835908.html
Copyright © 2020-2023  润新知