Android 数据存取试验 (一)

有些时候需要把从别的地方获取的数据以文件的形式存储在手机上,在需要的时候再读取出来,这便需要熟悉文件的I/O操作。
今天就简单的把文件的I/O操作的实验记录简单的描述一下。
方法主要参考:《深入浅出Google Andorid》一书。
 
需要了解的是每个应用程序包都会有一个私有的存储数据的目录(类似文件夹),只有属于该包的应用程序才能写入该目录空间,每个包应用程序的私有数据目录位于Android绝对路径/data/data/<包名>/目录中。除了私有数据目录应用程序还拥有/sdcard目录(即SD Card的写入权限)。文件系统中其他系统目录,第三方应用程序是不可写入的。稍后我们在终端下借用“adb shell”进入Android系统查看一下我们的文件写入是否真正成功。
 
照旧三部曲:xml文件配置界面,java程序实现业务逻辑,然后再实践检验一下。
 
Step1、界面布局:依然是layout/main.xml
 
<? xml version ="1.0" encoding ="utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
         android:orientation ="vertical"
         android:layout_width ="fill_parent"
         android:layout_height ="fill_parent"
         android:background ="#ffffff"
         >
    
   < TextView    
         android:layout_width ="fill_parent"    
         android:layout_height ="wrap_content"    
         android:text ="@string/hello"
         android:textColor ="#0000ff"
         />
    
   < EditText    
     android:hint ="Input here:"    
     android:id ="@+id/input"    
     android:layout_width ="fill_parent"    
     android:layout_height ="wrap_content"
     >
   </ EditText >
   < RelativeLayout android:layout_width ="fill_parent"
     android:layout_height ="wrap_content"
     >
     < Button android:text ="Store"    
       android:id ="@+id/btn_store"    
       android:layout_width ="wrap_content"    
       android:layout_height ="wrap_content" >
     </ Button >
     < Button    
       android:text ="Extract"    
       android:id ="@+id/btn_extract"    
       android:layout_width ="wrap_content"    
       android:layout_height ="wrap_content"
       android:layout_toRightOf ="@id/btn_store" >
     </ Button >
   </ RelativeLayout >
    
   < TextView    
     android:text ="The store info will be displayed here."    
     android:id ="@+id/display"    
     android:layout_width ="fill_parent"    
     android:layout_height ="wrap_content"
     android:textColor ="#0000ff"
     >
   </ TextView >
</ LinearLayout >
 
Step2、Java代码
package com.penguin7.filetest;

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

import org.apache.http.util.EncodingUtils;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class FileTest extends Activity {
   private final String FILE_PATH = "/data/data/com.penguin7.filetest/";
   private final String FILE_NAME = "filetest.txt";

   // Definition file
  File file;
  FileOutputStream out;
  FileInputStream in;

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

     // execute store action
    Button btnStore = (Button) findViewById(R.id.btn_store);
    btnStore.setOnClickListener( new View.OnClickListener() {

      @Override
       public void onClick(View arg0) {
         try {
           // Create a file
          file = new File(FILE_PATH, FILE_NAME);
          file.createNewFile();

           // Open output stream for the file
          out = new FileOutputStream(file);

           // Get the content and store into the file
          EditText et = (EditText) findViewById(R.id.input);
          String info = et.getText().toString();
           // Transform the string to byte array then store into file
          out.write(info.getBytes());
          out.close();
        } catch (FileNotFoundException e) {
           // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (IOException e) {
           // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    });

     // Execute read action
    Button btnExtract = (Button) findViewById(R.id.btn_extract);
    btnExtract.setOnClickListener( new View.OnClickListener() {

      @Override
       public void onClick(View arg0) {
         try {
           // Create a file object to prepare read file's content
          file = new File(FILE_PATH, FILE_NAME);
           if (!file.exists()) {
             return;
          }
           // Open input stream for file
          in = new FileInputStream(file);
           // Get length of file and apply to a byte array to store file's information
           int length = ( int) file.length();
           byte[] temp = new byte[length];
          in.read(temp, 0, length);
          TextView tv = (TextView) findViewById(R.id.display);
           // Before display the content we should transform to UTF-8 coding way
          tv.setText(EncodingUtils.getString(temp, "UTF-8"));
          in.close();
        } catch (FileNotFoundException e) {
           // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (IOException e) {
           // TODO Auto-generated catch block
          e.printStackTrace();
        }

      }
    });
  }
}
 
Step3、查看效果
1、启动程序
 
2、输入文字后,点击存储按钮,再点击提取按钮,效果图如下
 
 
3、最终使用adb shell查看文件是否真实存在并写入数据
 
 

你可能感兴趣的:(android,移动开发,职场,休闲,文件写入与读取)