您好,登錄后才能下訂單哦!
Android系統(tǒng)在文件IO操作上主要還是采用Java中的iava.io.FileInputStream和java.io.FileOutputStream來對(duì)文件進(jìn)行讀寫操作,創(chuàng)建文件或文件夾使用java.io.File類來完成,同時(shí)讀寫文件需要相應(yīng)的權(quán)限,否則將會(huì)出現(xiàn)Exception。
Android系統(tǒng)本身提供了2個(gè)方法用于文件的讀寫操作:
openFileInput(String name)方法返回FileIputStream上輸入流和openFileOutput(String name,int mode)方法返回FileOutputStream輸出流。
這兩個(gè)方法只支持操作當(dāng)前應(yīng)用的私有目錄下的文件,傳入文件時(shí)只需傳入文件名即可。對(duì)于存在的文件,默認(rèn)使用覆蓋私有模式(Context.MODE_PRIVATE)對(duì)文件進(jìn)行寫操作,如果想以增量方式寫入一寸文件,需要指定輸出模式為Context.MODE_APPEND.
下面為文件操作簡(jiǎn)單小例子:
FileActivity.java代碼:
public class FileActivity extends Activity {
private static final String FILE_NAME="test.txt";
private EditText edittext;
private Button button1,button2;
private TextView text;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file);
edittext=(EditText)findViewById(R.id.edittext);
button1=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
text=(TextView)findViewById(R.id.text);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
saveDateToFile();
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
readDateFile();
}
});
}
private void saveDateToFile(){
try {
//FileOutputStream fos=openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
//構(gòu)造輸出流對(duì)象,使用覆蓋私有模式
FileOutputStream fos=openFileOutput(FILE_NAME, Context.MODE_APPEND);
//構(gòu)造輸出流對(duì)象,使用增量模式
String textfile=edittext.getText().toString();
//獲取輸入內(nèi)容
fos.write(textfile.getBytes());
//將字符串轉(zhuǎn)換為byte數(shù)組寫入文件
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void readDateFile(){
try {
FileInputStream fis=openFileInput(FILE_NAME);
byte[] buffer=new byte[1024];
//構(gòu)建byte數(shù)組用于保存讀入的數(shù)據(jù)
fis.read(buffer);
//讀取數(shù)據(jù)放在buffer中
String textfile=EncodingUtils.getString(buffer,"UTF-8");
text.setText(textfile);
text.setTextColor(Color.RED);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
寫操作就是saveDateToFile()方法,讀操作就是readDateFile()方法。
xml文件很簡(jiǎn)單,幾個(gè)控件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FileActivity" >
<EditText
android:id="@+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="請(qǐng)輸入..." />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/edittext"
android:text="存入" />
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:text="文件內(nèi)容:" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edittext"
android:layout_toRightOf="@+id/button1"
android:text="讀出" />
</RelativeLayout>
創(chuàng)建的test.txt文件就在SD卡目錄下面。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。