安卓数据存储方式之IO存储
2017-06-23 18:42
211 查看
1.知识图谱:
XML代码:
JAVA代码:
存储在手机内存卡里面要在manifests下的AndroidManifest.xml里面给权限:<
4000
/p>
存储在手机内存里面的查看文件的路径是:
在夜神模拟器里面的查看是:文件管理------》data--------->data---------->项目的包名------------》files-------------->文件名
存储在手机内存卡里面查看文件的路径是:
文件管理------>mnt---------->sdcard---------->文件名
XML代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.cookie.android0623data.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et_main_name" android:hint="请输入用户名"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:lines="5" android:id="@+id/et_main_text"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="保存" android:onClick="save"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="打开" android:onClick="read"/> </LinearLayout> </LinearLayout>
JAVA代码:
package com.example.cookie.android0623data; import android.content.Context; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class MainActivity extends AppCompatActivity { private EditText et_main_text; private EditText et_main_name; private String sdCard; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_main_text = (EditText) findViewById(R.id.et_main_text); et_main_name = (EditText) findViewById(R.id.et_main_name); //获取手机内存卡的路径 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ sdCard = Environment.getExternalStorageDirectory().getAbsolutePath(); } } public void save(View view){ String content=et_main_text.getText().toString(); String fileName=et_main_name.getText().toString(); //存东西,输出流OutputStream try { //存储在手机内存里面 //FileOutputStream fos=openFileOutput(fileName, Context.MODE_PRIVATE); //存储在内存卡里面 FileOutputStream fos=new FileOutputStream(sdCard+"/"+fileName); fos.write(content.getBytes()); fos.close(); Toast.makeText(this, "保存成功!", Toast.LENGTH_SHORT).show(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void read(View view){ String fileName=et_main_name.getText().toString(); //读,输入流Input try { //存储在手机内存里 // FileInputStream fis= openFileInput(fileName); //存储在手机内存卡里面 FileInputStream fis=new FileInputStream(sdCard+"/"+fileName); byte buf[]=new byte[1024]; int len=0; StringBuffer s=new StringBuffer(); while((len=fis.read(buf))!=-1){ s.append(new String(buf,0,len)); } et_main_text.setText(s); fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
存储在手机内存卡里面要在manifests下的AndroidManifest.xml里面给权限:<
4000
/p>
<!-- 内存卡读和写的权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
存储在手机内存里面的查看文件的路径是:
在夜神模拟器里面的查看是:文件管理------》data--------->data---------->项目的包名------------》files-------------->文件名
存储在手机内存卡里面查看文件的路径是:
文件管理------>mnt---------->sdcard---------->文件名
相关文章推荐
- 安卓数据存储方式之IO存储
- 安卓突击:数据存储方式
- 安卓中的5种数据存储方式
- 安卓向SD卡存储数据时java.io.FileNotFoundException:(Permission denied)
- 数据存储方式(IO-SharedPreferences)
- 安卓数据存储方式
- 安卓数据存储方式
- 安卓常用数据存储方式之数据库存储
- 安卓数据存储方式之SQLite
- 安卓学习-数据存储与IO-SharedPreferences
- 安卓中常用的数据存储方式之普通文件存取
- 安卓存储数据和文件系列2:共享参数(sharedpreferences)方式
- 安卓数据存储方式之SQLite
- 安卓存储数据和文件系列3:SQLite数据库方式
- 安卓数据存储的几种方式
- 安卓存储数据的4种方式
- 安卓中常用的数据存储方式之配置文件存取
- 数据存储方式(IO-SharedPreferences)
- Android-数据存储方式(IO-SharedPreferences)
- 安卓中的数据存储方式以及ContentProvider的简单介绍