Android系統(tǒng)在文件IO操作上主要還是采用Java中的iava.io.FileInputStream和java.io.FileOutputStream來(lái)對(duì)文件進(jìn)行讀寫(xiě)操作,創(chuàng)建文件或文件夾使用java.io.File類(lèi)來(lái)完成,同時(shí)讀寫(xiě)文件需要相應(yīng)的權(quán)限,否則將會(huì)出現(xiàn)Exception。
創(chuàng)新互聯(lián)致力于互聯(lián)網(wǎng)品牌建設(shè)與網(wǎng)絡(luò)營(yíng)銷(xiāo),包括成都網(wǎng)站制作、成都做網(wǎng)站、SEO優(yōu)化、網(wǎng)絡(luò)推廣、整站優(yōu)化營(yíng)銷(xiāo)策劃推廣、電子商務(wù)、移動(dòng)互聯(lián)網(wǎng)營(yíng)銷(xiāo)等。創(chuàng)新互聯(lián)為不同類(lèi)型的客戶提供良好的互聯(lián)網(wǎng)應(yīng)用定制及解決方案,創(chuàng)新互聯(lián)核心團(tuán)隊(duì)10年專(zhuān)注互聯(lián)網(wǎng)開(kāi)發(fā),積累了豐富的網(wǎng)站經(jīng)驗(yàn),為廣大企業(yè)客戶提供一站式企業(yè)網(wǎng)站建設(shè)服務(wù),在網(wǎng)站建設(shè)行業(yè)內(nèi)樹(shù)立了良好口碑。
Android系統(tǒng)本身提供了2個(gè)方法用于文件的讀寫(xiě)操作:
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)行寫(xiě)操作,如果想以增量方式寫(xiě)入一寸文件,需要指定輸出模式為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ù)組寫(xiě)入文件
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();
}
}
}
寫(xiě)操作就是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卡目錄下面。
文章題目:如何進(jìn)行文件IO操作
文章路徑:http://aaarwkj.com/article40/jjgjeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、網(wǎng)站改版、外貿(mào)網(wǎng)站建設(shè)、App設(shè)計(jì)、網(wǎng)站設(shè)計(jì)公司、軟件開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)