安卓数据存储方式之IO存储

来源:互联网 发布:sql server r2 32位 编辑:程序博客网 时间:2024/05/18 00:17

1.知识图谱:


XML代码:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  5.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:orientation="vertical"  
  8.     tools:context="com.example.cookie.android0623data.MainActivity">  
  9.   
  10.     <EditText  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:id="@+id/et_main_name"  
  14.         android:hint="请输入用户名"/>  
  15.     <EditText  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:lines="5"  
  19.         android:id="@+id/et_main_text"/>  
  20.     <LinearLayout  
  21.         android:layout_width="match_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:orientation="vertical">  
  24.     <Button  
  25.         android:layout_width="match_parent"  
  26.         android:layout_height="wrap_content"  
  27.         android:text="保存"  
  28.         android:onClick="save"/>  
  29.   
  30.     <Button  
  31.         android:layout_width="match_parent"  
  32.         android:layout_height="wrap_content"  
  33.         android:text="打开"  
  34.         android:onClick="read"/>  
  35.     </LinearLayout>  
  36.   
  37. </LinearLayout>  



Java代码:

[html] view plain copy
  1. package com.example.cookie.android0623data;  
  2.   
  3. import android.content.Context;  
  4. import android.os.Environment;  
  5. import android.support.v7.app.AppCompatActivity;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.EditText;  
  9. import android.widget.Toast;  
  10.   
  11. import java.io.FileInputStream;  
  12. import java.io.FileNotFoundException;  
  13. import java.io.FileOutputStream;  
  14. import java.io.IOException;  
  15.   
  16. public class MainActivity extends AppCompatActivity {  
  17.   
  18.     private EditText et_main_text;  
  19.     private EditText et_main_name;  
  20.     private String sdCard;  
  21.   
  22.     @Override  
  23.     protected void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.activity_main);  
  26.         et_main_text = (EditText) findViewById(R.id.et_main_text);  
  27.         et_main_name = (EditText) findViewById(R.id.et_main_name);  
  28.         //获取手机内存卡的路径  
  29.         if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){  
  30.             sdCard = Environment.getExternalStorageDirectory().getAbsolutePath();  
  31.         }  
  32.   
  33.     }  
  34.   
  35.     public void save(View view){  
  36.         String content=et_main_text.getText().toString();  
  37.         String fileName=et_main_name.getText().toString();  
  38.         //存东西,输出流OutputStream  
  39.         try {  
  40.             //存储在手机内存里面  
  41.             //FileOutputStream fos=openFileOutput(fileName, Context.MODE_PRIVATE);  
  42.             //存储在内存卡里面  
  43.             FileOutputStream fos=new FileOutputStream(sdCard+"/"+fileName);  
  44.             fos.write(content.getBytes());  
  45.             fos.close();  
  46.             Toast.makeText(this, "保存成功!", Toast.LENGTH_SHORT).show();  
  47.         } catch (FileNotFoundException e) {  
  48.             e.printStackTrace();  
  49.         } catch (IOException e) {  
  50.             e.printStackTrace();  
  51.         }  
  52.   
  53.     }  
  54.   
  55.     public void read(View view){  
  56.         String fileName=et_main_name.getText().toString();  
  57.         //读,输入流Input  
  58.         try {  
  59.             //存储在手机内存里  
  60.           // FileInputStream fisopenFileInput(fileName);  
  61.             //存储在手机内存卡里面  
  62.             FileInputStream fis=new FileInputStream(sdCard+"/"+fileName);  
  63.             byte buf[]=new byte[1024];  
  64.             int len=0;  
  65.             StringBuffer s=new StringBuffer();  
  66.             while((len=fis.read(buf))!=-1){  
  67.                 s.append(new String(buf,0,len));  
  68.             }  
  69.             et_main_text.setText(s);  
  70.             fis.close();  
  71.         } catch (FileNotFoundException e) {  
  72.             e.printStackTrace();  
  73.         } catch (IOException e) {  
  74.             e.printStackTrace();  
  75.         }  
  76.     }  
  77. <span style="font-size:14px;"><strong>}</strong></span>  

存储在手机内存卡里面要在manifests下的AndroidManifest.xml里面给权限:

[html] view plain copy
  1. <!-- 内存卡读和写的权限 -->  
  2. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
  3. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />  


存储在手机内存里面的查看文件的路径是:

在夜神模拟器里面的查看是:文件管理------》data--------->data---------->项目的包名------------》files-------------->文件名


存储在手机内存卡里面查看文件的路径是:

文件管理------>mnt---------->sdcard---------->文件名