先看效果:
第一步先把工程建立起来然后布置成如下状态:
xml布局方式采用线性布局。
点击(此处)折叠或打开
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- >
-
- android:id="@+id/editText"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:hint="这里输入要保存的数据"
- />
-
- android:id="@+id/writebnt"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="写入数据" />
-
- android:id="@+id/readbnt"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="读取数据" />
-
- android:id="@+id/show"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="这里显示读出的数据"
- />
第二步:
完成文件创建和写入和读取这两三个功能代码如下:
点击(此处)折叠或打开
- package com.nguhyw.sdcard;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.UnsupportedEncodingException;
- import android.os.Bundle;
- import android.os.Environment;
- import android.app.Activity;
- import android.view.Menu;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private EditText edit;
- private TextView show;
- private Button writebnt;
- private Button readbnt;
- byte[] buff1 = new byte[10];
- char[] buff2 = new char[10];
- File sdcard = Environment.getExternalStorageDirectory();
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- edit = (EditText) findViewById(R.id.editText);
- show = (TextView) findViewById(R.id.show);
- writebnt = (Button) findViewById(R.id.writebnt);
- readbnt = (Button) findViewById(R.id.readbnt);
- writebnt.setOnClickListener(new View.OnClickListener() {
- public void onClick(View arg0) {
- File myfile = new File(sdcard,"Temple.fptH");
- if(!sdcard.exists()){
- Toast.makeText(getApplicationContext(), "当前系统不具备SD卡", Toast.LENGTH_SHORT).show();
- return ;
- }
- for(int i=0;i<10;i++){buff1[i]=0x38;buff2[i]=(char) buff1[i];}
- try {
- myfile.createNewFile();
- Toast.makeText(getApplicationContext(), "文件创建完成", Toast.LENGTH_SHORT).show();
- FileOutputStream fos = new FileOutputStream(myfile);
- OutputStreamWriter osw = new OutputStreamWriter(fos);
- osw.write(buff2);
- osw.flush();
- osw.close();
- fos.close();
- Toast.makeText(getApplicationContext(), "数据写入完成", Toast.LENGTH_SHORT).show();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- });
- readbnt.setOnClickListener(new View.OnClickListener() {
- public void onClick(View arg0) {
- File myfile = new File(sdcard,"Temple.fptH");
- if(myfile.exists()){
- try {
- FileInputStream fis =new FileInputStream(myfile);
- InputStreamReader isr =new InputStreamReader(fis);
- char[] input = new char[fis.available()];
- isr.read(input);
- isr.close();
- fis.close();
- String str = new String(input);
- show.setText(str);
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- });
- }
- }
第四步设置应用的权限:
即读写Sd卡的权限
点击ADD添加权限:选择用户权限
然后在name哪里找的相对于的权限
需要两个权限,那就是Sd卡的读取和写入。