Chinaunix首页 | 论坛 | 博客
  • 博客访问: 225497
  • 博文数量: 80
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 781
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-08 10:41
个人简介

爱莉清

文章分类

全部博文(80)

文章存档

2018年(1)

2017年(18)

2016年(49)

2015年(7)

2014年(5)

我的朋友

分类: Android平台

2016-05-06 08:56:25

先看效果:

第一步先把工程建立起来然后布置成如下状态:
xml布局方式采用线性布局。

  

点击(此处)折叠或打开

  1. xmlns:android=""
  2.     xmlns: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卡的权限


点击ADD添加权限:选择用户权限

然后在name哪里找的相对于的权限


需要两个权限,那就是Sd卡的读取和写入。
全部代码如下:
SD卡文件读写.zip

另一种方法:


用于字节行的数据。


阅读(4137) | 评论(0) | 转发(0) |
0

上一篇:Windows下的串口编程系列之一(编程初探)

下一篇:C++ 串口编程 错误汇总

给主人留下些什么吧!~~