2012年3月18日 星期日
Context IO 私有資料夾的檔案寫入和讀取
私有資料夾(Context) 的檔案寫入和讀取: /data/data/com.example.package.io
利用下述的方法:
openFileInput (String fileName); 讀出資料
openFileOutput (String name, int mode); 寫入資料
fileList ();
deleteFile(String fileName);
main.java:
public class IO01Activity extends Activity {
/** Called when the activity is first created. */
public static final String ENCORDING = "UTF-8";
String fileName = "test01.txt"; //設定檔案
String message = "一開始寫入的資料";
TextView tv;
EditText et;
Button inputBtn;
Button deleteInputBtn;
Button editBtn;
Button deleteFileBtn;
InputMethodManager inputMgr;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)findViewById(R.id.textView01);
et = (EditText)findViewById(R.id.editText1);
inputBtn = (Button)findViewById(R.id.button1);
deleteInputBtn = (Button)findViewById(R.id.button3);
editBtn = (Button)findViewById(R.id.button4);
deleteFileBtn = (Button)findViewById(R.id.button2);
// inputMgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
// inputMgr.toggleSoftInput(0, 0);
//重新輸入內容
deleteInputBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
et.setText("");
}
});
//完成,顯示檔案內容
inputBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String message = et.getText().toString(); //拿到使用者輸入的字串
writeFileData (fileName, message); //寫進去檔案test01.txt
//完成時將keyboard 隱藏,以不至於檔到螢幕的View
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(et.getWindowToken(), 0);
//將結果顯示在螢幕上
String result = readFileData (fileName); //去test01.txt取出內容,顯示在螢幕上
tv.setText(result);
et.setText("");// 清空 editText box
}
});
//修改檔案內容
editBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
deleteFile(fileName);
et.setText(tv.getText().toString());
}
});
//刪除檔案的全部內容
deleteFileBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
deleteFile(fileName);
tv.setText("新檔案:");
}
});
}
//方法:向指定檔案中寫入指定的資料
public void writeFileData (String fileName, String message){
try {
FileOutputStream fos = openFileOutput(fileName, MODE_APPEND);
byte[] bytes = message.getBytes();//將要寫入的字串轉換為byte陣列
fos.write(bytes); //將byte陣列寫入檔案
fos.close(); //關閉FileOutputStream物件
} catch (Exception e) {
e.printStackTrace(); //捕獲異常並列印
}
}// end of writeFileData
//方法:打開指定檔,讀取其資料,返回字串物件
public String readFileData (String fileName){
String result = "";
try {
FileInputStream fis = openFileInput(fileName);
int length = fis.available(); //獲取檔案長度
byte[] buffer = new byte [length]; //新建byte陣列用於讀入資料
fis.read(buffer); //將檔案內容讀入到byte陣列中
result = EncodingUtils.getString(buffer, ENCORDING);//將byte陣列轉換成指定格式的字串
fis.close(); //關閉檔案輸入串流
} catch (Exception e) {
e.printStackTrace(); //捕獲異常並列印
}
return result; //返回讀到的資料字串
}
}
*********************************************
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" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</EditText>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重新輸入input格的內容" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="完成,顯示檔案內容" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改檔案內容" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="刪除此檔案的全部內容" />
<TextView
android:id="@+id/textView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="新檔案:" />
</LinearLayout>
**********************************************
也可以將資料檔存在 res/raw/test1.txt or assets/test2.txt 如下:
但只可以讀出。
利用:
public class IORawAssetsActivity extends Activity {
/** Called when the activity is first created. */
public static final String ENCODING = "UTF-8";
TextView tv1, tv2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1 = (TextView) findViewById(R.id.textView1);
tv2 = (TextView) findViewById(R.id.textView2);
tv1.setText(getFromRaw("test1.txt"));
tv2.setText(getFromAssets("test2.txt"));
}
//方法:從resource中的raw資料夾中獲取檔案並讀取資料
public String getFromRaw (String fileName) {
String result = "";
try {
InputStream is = getResources().openRawResource(R.raw.test1);//從raw中取檔案
int length = is.available();//獲取檔案的位元組數
byte [] buffer = new byte [length];//新建byte陣列
is.read(buffer);//將檔案中的資料讀取到byte陣列中
result = EncodingUtils.getString(buffer, ENCODING);//將byte陣列轉換成指定格式的字串
is.close();//關閉
}catch (Exception e) {
e.printStackTrace();
}
return result;
}// end of getFromRaw()
or from assets:
public String getFromAssets(String fileName) {
String result = "";
try {
InputStream is = getResources().getAssets().open(fileName);
int length = is.available();
byte [] buffer = new byte [length];
is.read(buffer);
result = EncodingUtils.getString(buffer, ENCODING);
}catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
參考:
http://blog.csdn.net/dww410/article/details/5399094
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言