2012年5月4日 星期五

IO: (3) 讀取SDCARD/fileio裡某個檔案的內容


IO


//讀取SDCARD/fileio裡檔案的內容


res/layout/main.xml:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:singleLine="false"
        android:maxLines="15"
        android:text="@string/hello"
        android:textSize="20dp"
        android:textColor="#00FF00" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

**************************************************************


public class IO_FileContentActivity extends Activity {
    /** Called when the activity is first created. */

private final String SD_PATH = Environment.getExternalStorageDirectory().getAbsolutePath();
private final String FILE_PATH = "/fileio";
private final String FILE_NAME = "test03.txt";
private final String ENCODING = "UTF-8";

FileInputStream fis = null;

TextView tv;
ListView lv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
         tv = (TextView) findViewById(R.id.textView);
         tv.setMovementMethod(ScrollingMovementMethod.getInstance());//text 太長的話,讓TextView 滾動
     
        //列出Sdcard 裡的夾子和檔案,放上LISTVIEW
        try {
        File sdcardPATH = Environment.getExternalStorageDirectory();//找出SD的入徑
//         String [] fileNamesArrayStr = sdcardPATH.list(); //用list(),將SD裡的資料夾和檔案以字串的方式收集在array
       
//         File [] fileArray = sdcardPATH.listFiles(); //listFiles()則會加上完整入徑,列出入徑及資料夾+檔名的集合,存在File格是的陣列

        File myDataPath = new File (sdcardPATH.getAbsolutePath() + "/fileio");
        File [] fileArray = myDataPath.listFiles();
       
       
        String  tempStr = ""; //初始化
       
        for (int i=0; i < fileArray.length; i++) {
        if (fileArray[i].isFile()) {   //只收集檔案,不要資料夾,但每筆資料會產生入徑(但無法用 fileNamesArrayStr來做,因為它是字串,無法判定是否為檔案還是資料夾
        String str = ""; //用來接收每一筆的檔案名字串,但是會包含前面的入徑:/mnt/sdcard/fileio
        str = fileArray[i].toString();
        String strSub = "";// 用來去除多餘的入徑字串:/mnt/sdcard/fileio
        strSub = str.substring(19, str.length()).toString(); //利用substring 去掉每筆最前面的19個字元:/mnt/sdcard/fileio
        tempStr += strSub + "--" ; // 以 "--"為記號,分隔每筆處理好的資料,存成一筆字串
       
        }
        }
        String result = ""; //設一變數來處理最後的"--"兩個字元,讓它不會在ListView上多產生一個空的cell
        result = tempStr.substring(0, tempStr.length()-2);
        final String[] strings = TextUtils.split(result, "--"); //以"--"為分節點將整筆字串切割成數個字串儲存成字串陣列,以顯示在螢幕上
       
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings);
        lv = (ListView) findViewById(R.id.listView1);
        lv.setAdapter(adapter);
       
        lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String filename = strings[position].toString();
Toast.makeText(Cq_IO_File01Activity.this, filename, Toast.LENGTH_SHORT).show();
readFile(filename);
}
});
       
       
        } catch (Exception e) {
        e.printStackTrace();
        }
 
     
     
     
        //readFile(); //讀出指定的檔到TEXTVIEW上
     
    }// end of onCreate
 
 
 
    public void readFile (String filename) {
   
    String result = "";
    try {
   
    File mFilePath = new File (SD_PATH + FILE_PATH);
    String mFileName = mFilePath + "/" + filename;
    File fileText = new File (mFileName);
   
    fis = new FileInputStream(fileText);
    int length = fis.available();
    byte [] buffer = new byte [length];
    fis.read(buffer);
    result = EncodingUtils.getString(buffer, ENCODING);
    fis.close();
    tv.setText(result);
   
    } catch (Exception e) {
    e.printStackTrace();
    }
    }// end of readFile()
}

沒有留言:

張貼留言