Android SD卡创建文件夹以及读取文件

package gongzibai.co.cc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;

public class SD1Activity extends
  Activity {
 EditText editText1;
 private final String SD_PATH = android.os.Environment
   .getExternalStorageDirectory()
   .getAbsolutePath();

//设置文件夹

 public static final String FILE_PATH = "/loveyou1";

 private File myFilePath;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(
   Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  editText1 = (EditText) findViewById(R.id.editText1);

//不存在则创建
  myFilePath = new File(SD_PATH
    + FILE_PATH);

  if (!myFilePath.exists()) {
   myFilePath.mkdirs();

  }

  final ImageView imageView1 = (ImageView) findViewById(R.id.imageView1);

  findViewById(R.id.button1)
    .setOnClickListener(
      new OnClickListener() {

       @Override
       public void onClick(
         View v) {
        // TODO Auto-generated method stub
        try {

//写入文件名,实例化变量

         File file = new File(
           SD_PATH
             + FILE_PATH
             + "/"
             + "abc.txt");

//将写入的文件定义成类
         FileOutputStream fileOutputStream = new FileOutputStream(
           file);
         fileOutputStream
           .write(editText1
             .getText()
             .toString()
             .getBytes());
         fileOutputStream
           .close();

         editText1
           .setText("");
        }

        catch (Exception e) {
         // TODO: handle exception
        }

       }

      });

 }

 public boolean onKeyDown(
   int keyCode, KeyEvent event) {
  if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {

   try {
    File file = new File(
      SD_PATH
        + FILE_PATH
        + "/"
        + "abc.txt");
    FileInputStream fileInputStream = new FileInputStream(
      file);

    int length = (int) file
      .length();

    byte[] message = new byte[length];

    fileInputStream
      .read(message);
    fileInputStream.close();

    String msg01 = "";
    for (int i = 0; i < message.length; i++) {
     char c = (char) message[i];
     msg01 = msg01 + c;
    }
    editText1
      .setText(msg01);
   } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }

   return true;

  } else {

   return super.onKeyDown(
     keyCode, event);

  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值