Android mobile application foundation tutorial [data storage]

FileOutputStream fos = openFileOutput(String name, int mode);
//参数是文件名和文件的操作模式
//打开应用程序中对应的输出流,将数据存储到指定的文件
FileInputStream fis = openFileInput(String name);
//打开应用程序对应的输入流,读取指定文件中的数据

String fileName = "data.txt";                       // 文件名称
String content = "helloworld";                     // 保存数据
FileOutputStream fos = openFileOutput(fileName, MODE_PRIVATE);
fos.write(content.getBytes());	                  //将数据写入文件
fos.close();                                      //关闭输出

String state = Environment.getExternalStorageState();  //获取外部设备的状态           
if (state.equals(Environment.MEDIA_MOUNTED)) {   //判断外部设备是否可用          
    File SDPath = Environment.getExternalStorageDirectory(); //获取SD卡目录
    File file = new File(SDPath, "data.txt");
    String data = "HelloWorld";
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(data.getBytes());
    fos.close();
}

String content = "";
    FileInputStream fis = null;
    fis = openFileInput("data.txt");    //获得文件输入流对象
    byte[] buffer = new byte[fis.available()];//创建缓冲区,并获取文件长度
    fis.read(buffer);     //将文件内容读取到buffer缓冲区
    content = new String(buffer);     //转换成字符串
     fis.close();		              //关闭输入流

String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
    File SDPath = Environment.getExternalStorageDirectory(); //获取SD卡路径
    File file = new File(SDPath, "data.txt");           //创建文件对象
    FileInputStream fis = null;
    BufferedReader br = null;
    fis = new FileInputStream(file);         //创建文件输入流对象
   //创建字符输入缓冲流的对象
    br = new BufferedReader(new InputStreamReader(fis));
   String data = br.readLine();              //读取数据
   br.close();                               //关闭字符输入缓冲流                                                 
   fis.close();                              //关闭输入流                                                     
}

<uses-permission    
                        android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

ActivityCompat.requestPermissions(MainActivity.this, //动态申请权限的方法,参数是上下文
new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"}, 1);

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, 
int[] grantResults) {
//申请权限的回调方法
//第一个参数requestCode为请求码
//第二个参数permissions为请求的权限
//第三个参数grantResults为用户授予权限的结果,当用户授予权限时,该数组中对应的值为PackageManager.PERMISSION_GRANTED
   	super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

This chapter mainly explains the data storage in Android. Firstly, it introduces the common data storage methods in Android, and then explains in detail the file storage, SharedPreferences storage and SQLite database storage. Data storage is a very important content in Android development. Generally, the knowledge of data storage is often involved in applications, Therefore, beginners must master the knowledge of this chapter.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>