数据存储—读写内部存储的文件数据

android studio中打开DDMS

内部存储空间位置:

导航栏Tools->"Android"->"Android Device Monitor"->File Explorer->data->data->工程目录

/***

可能出现DDMS无法连接相应设备的情况,原因是真机或genymotion的root权限未打开

这篇博客解释了原因:

http://blog.csdn.net/arex_efan/article/details/20008001

xda解释的原因及提供的方法

http://forum.xda-developers.com/showthread.php?t=2528952

可自行在网络上查找 genymotion-arm-translation_v1.1压缩包

直接拖拽到genymotion模拟器页面进行安装

(宝宝你的百度云里有)

重启后

**/

activity_main:

Button

android:id="@+id/writBtn"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="保存数据"/>

Button

android:id="@+id/readBtn"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="读取数据"/>

TextView

android:id="@+id/show"

android:layout_height="wrap_content"

android:layout_width="match_parent"/>


MainActivity.class:

privateEditTextet;

privateTextViewshow;

privateStringfilename="test";


protected voidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);


et= (EditText)findViewById(R.id.et);

show=(TextView)findViewById(R.id.show);

findViewById(R.id.writBtn).setOnClickListener(this);

findViewById(R.id.readBtn).setOnClickListener(this);

}

public voidonClick(View v) {

switch(v.getId()) {

caseR.id.writBtn:

try{

FileOutputStream fos =  openFileOutput(filename,Context.MODE_PRIVATE);

OutputStreamWriter osw =newOutputStreamWriter(fos,"UTF-8");

osw.write(et.getText().toString());

osw.flush();

fos.flush();

osw.close();

fos.close();

Toast.makeText(getApplicationContext(),"写入完成",Toast.LENGTH_LONG).show();

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(UnsupportedEncodingException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}

break;

/***

* 注意关闭顺序,先打开的后关闭

* fis.available 文件长度

*/

caseR.id.readBtn:

try{

FileInputStream fis = openFileInput(filename);

InputStreamReader iis =newInputStreamReader(fis,"UTF-8");

charinput[] =new char[fis.available()];

iis.read(input);

iis.close();

fis.close();

String readed =newString(input);

show.setText(readed);

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}break;default:break;}}




最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容