您的位置:首页 > 其它

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

2017-06-23 18:42 211 查看
1.知识图谱:



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---------->文件名
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: