Android之SD卡上的文件读取

来源:互联网 发布:做淘宝什么最赚钱 编辑:程序博客网 时间:2024/06/09 04:00
public class ReadSDCard extends Activity {
      String FILE_NAME = "/test1.txt" ;//SD卡的文件的路径
      Button writeBtn, readBtn ; // 一个写的按钮,一个读取的按钮
      EditText text1, text2 ;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
             // TODO Auto-generated method stub
             super.onCreate(savedInstanceState);
            setContentView(R.layout. activity_readsdcard);
             writeBtn = (Button) findViewById(R.id.btn_write);
             readBtn = (Button) findViewById(R.id.btn_read);
             text1 = (EditText) findViewById(R.id.edit_t1);
             text2 = (EditText) findViewById(R.id.edit_t2);
             writeBtn.setOnClickListener(new OnClickListener() {
                   @Override
                   public void onClick(View v) {
                        String msg = text1.getText().toString();
                        writeMSG(msg);
                         text1.setText("" );
                  }
            });
             readBtn.setOnClickListener(new OnClickListener() {

                   @Override
                   public void onClick(View v) {
                         try {
                               // 如果手机插入了SD卡,而且应用程序具有访问SD的权限(要在配置文件中注册)
                               if (Environment.getExternalStorageState().equals(Environment. MEDIA_MOUNTED)) {
                                     // 获取SD卡对应的存储目录
                                    File sdCardDir = Environment
                                                . getExternalStorageDirectory();
                                     // 获取指定文件对应的输入流
                                    FileInputStream fis = new FileInputStream(sdCardDir
                                                .getCanonicalFile() + FILE_NAME);
                                     // 将制定的流包装秤BufferedReader
                                    BufferedReader br = new BufferedReader(
                                                 new InputStreamReader(fis));
                                    StringBuilder sb = new StringBuilder("" );
                                    String line = null;
                                     // 循环读取文件的内容
                                     while ((line = br.readLine()) != null) {
                                          sb.append(line);
                                    }
                                    br.close();
                                     text2.setText(sb);
                              }
                        } catch (FileNotFoundException e) {
                              e.printStackTrace();
                        } catch (IOException e) {
                              e.printStackTrace();
                        }
                  }
            });

      }


       private void writeMSG(String content) {
             try {
                   if (Environment.getExternalStorageState().equals(Environment. MEDIA_MOUNTED)) {
                         // 获取SD卡目录
                        File sdCard = Environment.getExternalStorageDirectory();
                        File targeFile = new File(sdCard.getCanonicalFile() + FILE_NAME );
                         // 以指定文件创建RandomAccessFile对象
                        RandomAccessFile raf = new RandomAccessFile(targeFile, "rw" );
                         // 将文件记住的指针移动到最后(移到最后才能追加、不然只能覆盖原有的数据)
                        raf.seek(targeFile.length());
                        raf.write(content.getBytes());
                        System. out.println("写入成功" + raf);
                        raf.close();
                  }

            } catch (Exception e) {
            }
      }

}
0 0