Android进阶(七)数据存储_"file file = new file(getexternalfilesdir(null),

更多面试题

**《350页前端校招面试题精编解析大全》**内容大纲主要包括 HTML,CSS,前端基础,前端核心,前端进阶,移动端开发,计算机基础,算法与数据结构,项目,职业发展等等

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

此方法可以根据指定的目录类型创建目录,该文件为应用程序私有,如果卸载应用程序,目录将会一起删除。

API7一下,使用getExternalStorageDirectory(), 打开外部存储文件根目录 ,你的文件写入到如下目录中:/Android/data/<package_name>/files/
eg:
void createExternalStoragePrivateFile() {
    // Create a path where we will place our private file on external storage.
    File file = new File(getExternalFilesDir(null), “DemoFile.jpg”);
    try {
        // Very simple code to copy a picture from the application’s
        // resource into the external file.  Note that this code does
        // no error checking, and assumes the picture is small (does not
        // try to copy it in chunks).  Note that if external storage is
        // not currently mounted this will silently fail.
        InputStream is = getResources().openRawResource(R.drawable.balloons);
        OutputStream os = new FileOutputStream(file);
        byte[] data = new byte[is.available()];
        is.read(data);
        os.write(data);
        is.close();
        os.close();
    } catch (IOException e) {
        // Unable to create file, likely because external storage is not currently mounted.
        Log.w(“ExternalStorage”, “Error writing " + file, e);
    }
}
void deleteExternalStoragePrivateFile() {
    // Get path for the file on external storage. If external
    // storage is not currently mounted this will fail.
    File file = new File(getExternalFilesDir(null), “DemoFile.jpg”);
    if (file != null) {
        file.delete();
    }
}
boolean hasExternalStoragePrivateFile() {
    // Get path for the file on external storage.  If external
    // storage is not currently mounted this will fail.
    File file = new File(getExternalFilesDir(null), “DemoFile.jpg”);
    if (file != null) {
        return file.exists();
    }
    return false;
}
2.1.3保存为共享文件
如果想将文件保存为不为应用程序私有,在应用程序卸载时不被删除,需要将文件保存到外部存储的公共目录上,这些目录在存储设备根目录下;如:音乐,图片,铃声等。
API8以上,使用 getExternalStoragePublicDirectory(),传入一个公共目录的类型参数,如DIRECTORY_MUSIC, DIRECTORY_PICTURES, DIRECTORY_RINGTONES等, 目录不存在时这个方法会为你创建目录。
API7一下,使用 getExternalStorageDirectory() 打开存储文件根目录,保存文件到下面的目录中:
Music,Podcasts,Ringtones,Alarms,Notifications,Pictures,Movies,Download。
eg:
void createExternalStoragePublicPicture() {
    // Create a path where we will place our picture in the user’s public pictures directory. 
    File path = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File file = new File(path, “DemoPicture.jpg”);
    try {
        // Make sure the Pictures directory exists.
        path.mkdirs();
        InputStream is = getResources().openRawResource(R.drawable.balloons);
        OutputStream os = new FileOutputStream(file);
        byte[] data = new byte[is.available()];
        is.read(data);
        os.write(data);
        is.close();
        os.close();
    } catch (IOException e) {
         Log.w(“ExternalStorage”, “Error writing " + file, e);
    }
}
void deleteExternalStoragePublicPicture() {
    // Create a path where we will place our picture in the user’s
    // public pictures directory and delete the file.  If external
    // storage is not currently mounted this will fail.
    File path = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File file = new File(path, “DemoPicture.jpg”);
    file.delete();
}
boolean hasExternalStoragePublicPicture() {
    // Create a path where we will place our picture in the user’s
    // public pictures directory and check if the file exists.  If
    // external storage is not currently mounted this will think the
    // picture doesn’t exist.
    File path = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File file = new File(path, “DemoPicture.jpg”);
    return file.exists();
}
2.1.4保存缓存文件
API8以上,使用getExternalCacheDir() 打开存储目录保存文件,如卸载应用程序,缓存文件将自动删除,在应用程序运行期间你可以管理这些缓存文件,如不在使用可以删除以释放空间。
API7以下,使用getExternalStorageDirectory() 打开缓存目录,缓存文件保存在下面的目录中:
/Android/data/<package_name>/cache/
<package_name> 你的java包名如 “com.example.android.app”.
2.1.5读写文件
提示:openFileOutput是在raw里编译过的访问设备存储,FileOutputStream是任何文件都可以
访问sdcard直接实例FileOutputStream对象,而访问存储设备文件通过openFileOutput返回FileOutputStream对象对数据操作。
2.1.6 sdcard中去读文件
示例:
String fileName = “/sdcard/Y.txt”;
//也可以用String fileName = “mnt/sdcard/Y.txt”;
String res=””;     
try{ 
FileInputStream fin = new FileInputStream(fileName);
//FileInputStream fin = openFileInput(fileName);  
//用这个就不行了,必须用FileInputStream
    int length = fin.available(); 
    byte [] buffer = new byte[length]; 
    fin.read(buffer);     
    res = EncodingUtils.getString(buffer, “UTF-8”); 
    fin.close();     
    }catch(Exception e){ 
           e.printStackTrace(); 

myTextView.setText(res);
2.1.7 SDCard中写文件 
般写在\data\data\com.test\files\里面,打开DDMS查看file explorer是可以看到仿真器文件存放目录的结构的
String fileName = “TEST.txt”;
String message = “FFFFFFF11111FFFFF” ;
writeFileData(fileName, message);
   public voidwriteFileData(String fileName,String message){ 
       try{ 
        FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE);
        byte [] bytes = message.getBytes(); 
        fout.write(bytes); 
         fout.close(); 
        } 
       catch(Exception e){ 
        e.printStackTrace(); 
       } 
   }    
2.1.8 写,读sdcard目录上的文件,要用FileOutputStream, 不能用openFileOutput
//写在/mnt/sdcard/目录下面的文件
  public voidwriteFileSdcard(String fileName,String message){ 
      try{ 
       //FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);
      FileOutputStream fout = new FileOutputStream(fileName);
       byte [] bytes = message.getBytes(); 
       fout.write(bytes); 
        fout.close(); 
       } 
      catch(Exception e){ 
       e.printStackTrace(); 
      } 
  }
  //读在/mnt/sdcard/目录下面的文件
  public String readFileSdcard(String fileName){
       String res=“”; 
       try{ 
        FileInputStream fin = new FileInputStream(fileName); 
        int length = fin.available(); 
        byte [] buffer = new byte[length]; 
        fin.read(buffer);     
        res = EncodingUtils.getString(buffer, “UTF-8”); 
        fin.close();     
       } 
       catch(Exception e){ 
        e.printStackTrace(); 
       } 
       return res; 
  }
3android读写文件正确实行方法
Android读写文件正确实行方法介绍
http://www.ehuait.com/skill/android/2011-09-08/1445.html

众所周知Android有一套自己的安全模型, 具体可参见Android开发文档。当应用程序(.apk)在安装时就会分配一个userid,当该应用要去访问其他资源比如文件的时候,就需要userid匹配。

默认情况下,任何应用创建的文件,数据库,sharedpreferences都应该是私有的(位于/data/data/your_project/files/),其余程序无法访问。

除非在创建时指明是MODE_WORLD_READABLE 或者 MODE_WORLD_WRITEABLE,只要这样其余程序才能正确访问。

因为有这种Android读写文件的方法在安全上有所保障,进程打开文件时Android要求检查进程的user id。所以不能直接用java的api来打开,因为java的io函数没有提这个机制 。
无法用java的api直接打开程序私有的数据 ,默认路径为/data/data/your_project/files/

1.FileReader file = new FileReader(“Android.txt”); 这里特别强调私有数据!言外之意是如果某个文件或者数据不是程序私有的,即访问它时无须经过Android的权限检查,那么还是可以用java的io api来直接访问的。

所谓的非私有数据是只放在sdcard上的文件或者数据,可以用java的io api来直接打开sdcard上文件。

1.FileReader file = new FileReader(“/sdcard/Android.txt”); 如果要打开程序自己私有的文件和数据,那必须使用Activity提供openFileOutput和openFileInput方法。
创建程序私有的文件,由于权限方面的要求,必须使用activity提供的Android读写文件方法
1.FileOutputStream os = this.openFileOutput(“Android.txt”, MODE_PRIVATE);
2.OutputStreamWriter outWriter = new OutputStreamWriter (os);
读取程序私有的文件,由于权限方面的要求,必须使用activity提供的方法
1.FileInputStream os =this.openFileInput(“Android.txt”);
2.InputStreamReader inReader = new InputStreamReader(os); 
4Shared Preferences用户首选项
主要用于存放软件的配置参数等信息。sharedPreferences用于存取和修改软件配置参数数据的接口。存放键值对,保存用户个人首选项信息,比如喜爱音乐,主题,首选Activity等
4.1.1使用getSharedPrefernces()
使用步骤:
存放:
1.获得SharedPreferences 的实例对象,通过getSharedPreferences()传递文件名和模式;
2.获得Editor 的实例对象,通过SharedPreferences 的实例对象的edit()方法;
3.存入数据,利用Editor 对象的putXXX()方法;
4.提交修改的数据,利用Editor 对象的commit()方法。
读取:
1.获得SharedPreferences 的实例对象,通过getSharedPreferences()传递文件名和模式;
2.读取数据,通过SharedPreferences 的实例对象的getXXX()方法。
eg:
public class Calc extends Activity {
   public static final String PREFS_NAME = “MyPrefsFile”;
   @Override
   protected void onCreate(Bundle state){
      super.onCreate(state);
      . . .
      // Restore preferences

前端框架

前端框架太多了,真的学不动了,别慌,其实对于前端的三大马车,Angular、React、Vue 只要把其中一种框架学明白,底层原理实现,其他两个学起来不会很吃力,这也取决于你以后就职的公司要求你会哪一个框架了,当然,会的越多越好,但是往往每个人的时间是有限的,对于自学的学生,或者即将面试找工作的人,当然要选择一门框架深挖原理。

以 Vue 为例,我整理了如下的面试题。

Vue部分截图

如果你觉得对你有帮助,可以戳这里获取:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

找工作的人,当然要选择一门框架深挖原理。

以 Vue 为例,我整理了如下的面试题。

Vue部分截图

如果你觉得对你有帮助,可以戳这里获取:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

分析一下这个json {&quot;name&quot;:&quot;12312&quot;,&quot;project_id&quot;:&quot;87156&quot;,&quot;project_name&quot;:&quot;上上下下左左右右baba与聚法科技(长春)有限公司与公司、证券、保险、票据等有关的民事纠纷&quot;,&quot;client&quot;:&quot;[{&quot;type&quot;:&quot;自然人&quot;,&quot;customer_id&quot;:&quot;80236&quot;,&quot;customer_name&quot;:&quot;上上下下左左右右baba&quot;}]&quot;,&quot;sign_date&quot;:&quot;2023-06-06&quot;,&quot;expire_date&quot;:&quot;2023-06-21&quot;,&quot;subject_amount&quot;:&quot;123&quot;,&quot;contract_amount&quot;:&quot;123&quot;,&quot;charge_method&quot;:&quot;一次性,分阶段,风险,计时&quot;,&quot;equity_amount&quot;:&quot;13811&quot;,&quot;amount_info&quot;:&quot;[{&quot;type&quot;:&quot;一次性&quot;,&quot;pay_date&quot;:&quot;2023-07-03&quot;,&quot;charge_amount&quot;:&quot;12&quot;},{&quot;type&quot;:&quot;分阶段&quot;,&quot;pay_date&quot;:&quot;2023-06-13&quot;,&quot;charge_amount&quot;:&quot;123&quot;,&quot;is_satisfy&quot;:&quot;是&quot;,&quot;pay_condition&quot;:&quot;12312&quot;},{&quot;type&quot;:&quot;风险&quot;,&quot;pay_date&quot;:&quot;&quot;,&quot;charge_amount&quot;:&quot;&quot;,&quot;is_satisfy&quot;:&quot;是&quot;,&quot;pay_condition&quot;:&quot;123&quot;,&quot;basic_amount&quot;:&quot;123&quot;,&quot;risk_amount&quot;:&quot;12&quot;,&quot;object_amount&quot;:&quot;123123&quot;,&quot;object&quot;:&quot;赔偿金&quot;,&quot;risk_prop&quot;:&quot;13213&quot;,&quot;member&quot;:&quot;&quot;,&quot;rate&quot;:&quot;&quot;,&quot;hours&quot;:&quot;&quot;},{&quot;type&quot;:&quot;计时&quot;,&quot;member_id&quot;:&quot;392159&quot;,&quot;member&quot;:&quot;曹野&quot;,&quot;rate&quot;:&quot;11&quot;,&quot;hours&quot;:&quot;1231&quot;}]&quot;,&quot;seal_person&quot;:&quot;123&quot;,&quot;seal_type&quot;:&quot;律所公章,法人名章,财务章&quot;,&quot;seal_num&quot;:&quot;123&quot;,&quot;file_path&quot;:&quot;[{&quot;title&quot;:&quot;导入错误数据 (15).xls&quot;,&quot;path&quot;:&quot;382585/1686381522542/导入错误数据 (15).xls&quot;,&quot;size&quot;:&quot;91136&quot;},{&quot;title&quot;:&quot;3.txt&quot;,&quot;path&quot;:&quot;382585/1686561731102/3.txt&quot;,&quot;size&quot;:44078}]&quot;,&quot;remark&quot;:&quot;123123&quot;} 并使用php转换成字符串
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值