先看效果:
安卓文件操作 将文件读取出来存放到byte类型数组-LMLPHP

第一步先把工程建立起来然后布置成如下状态:
安卓文件操作 将文件读取出来存放到byte类型数组-LMLPHP
xml布局方式采用线性布局。

  

点击(此处)折叠或打开

  1. xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical"
  6.     >
  7.     
  8.         android:id="@+id/editText"
  9.         android:layout_width="fill_parent"
  10.         android:layout_height="wrap_content"
  11.         android:hint="这里输入要保存的数据"
  12.         />
  13.     
  14.         android:id="@+id/writebnt"
  15.         android:layout_width="fill_parent"
  16.         android:layout_height="wrap_content"
  17.         android:text="写入数据" />
  18.       
  19.         android:id="@+id/readbnt"
  20.         android:layout_width="fill_parent"
  21.         android:layout_height="wrap_content"
  22.         android:text="读取数据" />
  23.       
  24.           android:id="@+id/show"
  25.           android:layout_width="fill_parent"
  26.           android:layout_height="wrap_content"
  27.           android:text="这里显示读出的数据"
  28.           />

 




第二步:
完成文件创建和写入和读取这两三个功能代码如下:

点击(此处)折叠或打开

  1. package com.nguhyw.sdcard;

  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.io.OutputStreamWriter;
  9. import java.io.UnsupportedEncodingException;

  10. import android.os.Bundle;
  11. import android.os.Environment;
  12. import android.app.Activity;
  13. import android.view.Menu;
  14. import android.view.View;
  15. import android.widget.Button;
  16. import android.widget.EditText;
  17. import android.widget.TextView;
  18. import android.widget.Toast;

  19. public class MainActivity extends Activity {

  20.     private EditText edit;
  21.     private TextView show;
  22.     private Button writebnt;
  23.     private Button readbnt;
  24.     byte[] buff1 = new byte[10];
  25.     char[] buff2 = new char[10];
  26.     File sdcard = Environment.getExternalStorageDirectory();


  27.     protected void onCreate(Bundle savedInstanceState) {
  28.         super.onCreate(savedInstanceState);
  29.         setContentView(R.layout.activity_main);

  30.         edit = (EditText) findViewById(R.id.editText);
  31.         show = (TextView) findViewById(R.id.show);
  32.         writebnt = (Button) findViewById(R.id.writebnt);
  33.         readbnt = (Button) findViewById(R.id.readbnt);

  34.         writebnt.setOnClickListener(new View.OnClickListener() {

  35.             public void onClick(View arg0) {

  36.                 File myfile = new File(sdcard,"Temple.fptH");

  37.                 if(!sdcard.exists()){
  38.                     Toast.makeText(getApplicationContext(), "当前系统不具备SD卡", Toast.LENGTH_SHORT).show();
  39.                     return ;
  40.                 }
  41.                 for(int i=0;i<10;i++){buff1[i]=0x38;buff2[i]=(char) buff1[i];}
  42.                 try {
  43.                     myfile.createNewFile();
  44.                     Toast.makeText(getApplicationContext(), "文件创建完成", Toast.LENGTH_SHORT).show();
  45.                     FileOutputStream fos = new FileOutputStream(myfile);
  46.                     OutputStreamWriter osw = new OutputStreamWriter(fos);
  47.                     osw.write(buff2);
  48.                     osw.flush();
  49.                     osw.close();
  50.                     fos.close();
  51.                     Toast.makeText(getApplicationContext(), "数据写入完成", Toast.LENGTH_SHORT).show();

  52.                 } catch (IOException e) {
  53.                     e.printStackTrace();
  54.                 }

  55.             }
  56.         });

  57.         readbnt.setOnClickListener(new View.OnClickListener() {

  58.             public void onClick(View arg0) {
  59.                 File myfile = new File(sdcard,"Temple.fptH");
  60.                 if(myfile.exists()){
  61.                     try {
  62.                         FileInputStream fis =new FileInputStream(myfile);
  63.                         InputStreamReader isr =new InputStreamReader(fis);
  64.                         char[] input = new char[fis.available()];
  65.                         isr.read(input);
  66.                         isr.close();
  67.                         fis.close();
  68.                         String str = new String(input);
  69.                         show.setText(str);
  70.                     } catch (FileNotFoundException e) {
  71.                         // TODO Auto-generated catch block
  72.                         e.printStackTrace();
  73.                     } catch (UnsupportedEncodingException e) {
  74.                         // TODO Auto-generated catch block
  75.                         e.printStackTrace();
  76.                     } catch (IOException e) {
  77.                         // TODO Auto-generated catch block
  78.                         e.printStackTrace();
  79.                     }
  80.                 }
  81.             }
  82.         });


  83.     }



  84. }



第四步设置应用的权限:
即读写Sd卡的权限

安卓文件操作 将文件读取出来存放到byte类型数组-LMLPHP

点击ADD添加权限:选择用户权限
安卓文件操作 将文件读取出来存放到byte类型数组-LMLPHP

然后在name哪里找的相对于的权限
安卓文件操作 将文件读取出来存放到byte类型数组-LMLPHP


需要两个权限,那就是Sd卡的读取和写入。
全部代码如下:
安卓文件操作 将文件读取出来存放到byte类型数组-LMLPHPSD卡文件读写.zip

另一种方法:

安卓文件操作 将文件读取出来存放到byte类型数组-LMLPHP

用于字节行的数据。


09-21 15:04