andoird文件讀寫總結(jié)_第1頁
andoird文件讀寫總結(jié)_第2頁
andoird文件讀寫總結(jié)_第3頁
andoird文件讀寫總結(jié)_第4頁
andoird文件讀寫總結(jié)_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、Android - 文件讀寫操作 總結(jié)標(biāo)簽:androidstringbufferexceptionbytefile2012-03-05 20:2298690人閱讀評(píng)論(33)收藏舉報(bào)分類:Android(96)版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。在android中的文件放在不同位置,它們的讀取方式也有一些不同。本文對android中對資源文件的讀取、數(shù)據(jù)區(qū)文件的讀取、SD卡文件的讀取及RandomAccessFile的方式和方法進(jìn)行了整理。供參考。一、資源文件的讀?。?1) 從resource的raw中讀取文件數(shù)據(jù):javaview plaincopy1. Stringres

2、=;2. try3. 4. /得到資源中的Raw數(shù)據(jù)流5. InputStreamin=getResources().openRawResource(R.raw.test);6. 7. /得到數(shù)據(jù)的大小8. intlength=in.available();9. 10. bytebuffer=newbytelength;11. 12. /讀取數(shù)據(jù)13. in.read(buffer);14. 15. /依test.txt的編碼類型選擇合適的編碼,如果不調(diào)整會(huì)亂碼16. res=EncodingUtils.getString(buffer,BIG5);17. 18. /關(guān)閉19. in.clos

3、e();20. 21. catch(Exceptione)22. e.printStackTrace();23. 2) 從resource的asset中讀取文件數(shù)據(jù)javaview plaincopy1. StringfileName=test.txt;/文件名字2. Stringres=;3. try4. 5. /得到資源中的asset數(shù)據(jù)流6. InputStreamin=getResources().getAssets().open(fileName);7. 8. intlength=in.available();9. bytebuffer=newbytelength;10. 11. i

4、n.read(buffer);12. in.close();13. res=EncodingUtils.getString(buffer,UTF-8);14. 15. catch(Exceptione)16. 17. e.printStackTrace();18. 19. 二、讀寫/data/data/目錄上的文件:javaview plaincopy1. /寫數(shù)據(jù)2. publicvoidwriteFile(StringfileName,Stringwritestr)throwsIOException3. try4. 5. FileOutputStreamfout=openFileOutpu

5、t(fileName,MODE_PRIVATE);6. 7. bytebytes=writestr.getBytes();8. 9. fout.write(bytes);10. 11. fout.close();12. 13. 14. catch(Exceptione)15. e.printStackTrace();16. 17. 18. 19. /讀數(shù)據(jù)20. publicStringreadFile(StringfileName)throwsIOException21. Stringres=;22. try23. FileInputStreamfin=openFileInput(fileN

6、ame);24. intlength=fin.available();25. bytebuffer=newbytelength;26. fin.read(buffer);27. res=EncodingUtils.getString(buffer,UTF-8);28. fin.close();29. 30. catch(Exceptione)31. e.printStackTrace();32. 33. returnres;34. 35. 三、讀寫SD卡中的文件。也就是/mnt/sdcard/目錄下面的文件 :javaview plaincopy1. /寫數(shù)據(jù)到SD中的文件2. publicv

7、oidwriteFileSdcardFile(StringfileName,Stringwrite_str)throwsIOException3. try4. 5. FileOutputStreamfout=newFileOutputStream(fileName);6. bytebytes=write_str.getBytes();7. 8. fout.write(bytes);9. fout.close();10. 11. 12. catch(Exceptione)13. e.printStackTrace();14. 15. 16. 17. 18. /讀SD中的文件19. publicS

8、tringreadFileSdcardFile(StringfileName)throwsIOException20. Stringres=;21. try22. FileInputStreamfin=newFileInputStream(fileName);23. 24. intlength=fin.available();25. 26. bytebuffer=newbytelength;27. fin.read(buffer);28. 29. res=EncodingUtils.getString(buffer,UTF-8);30. 31. fin.close();32. 33. 34.

9、catch(Exceptione)35. e.printStackTrace();36. 37. returnres;38. 四、使用File類進(jìn)行文件的讀寫:javaview plaincopy1. /讀文件2. publicStringreadSDFile(StringfileName)throwsIOException3. 4. Filefile=newFile(fileName);5. 6. FileInputStreamfis=newFileInputStream(file);7. 8. intlength=fis.available();9. 10. bytebuffer=newb

10、ytelength;11. fis.read(buffer);12. 13. res=EncodingUtils.getString(buffer,UTF-8);14. 15. fis.close();16. returnres;17. 18. 19. /寫文件20. publicvoidwriteSDFile(StringfileName,Stringwrite_str)throwsIOException21. 22. Filefile=newFile(fileName);23. 24. FileOutputStreamfos=newFileOutputStream(file);25. 26

11、. bytebytes=write_str.getBytes();27. 28. fos.write(bytes);29. 30. fos.close();31. 五、另外,F(xiàn)ile類還有下面一些常用的操作:javaview plaincopy1. StringName=File.getName();/獲得文件或文件夾的名稱:2. StringparentPath=File.getParent();/獲得文件或文件夾的父目錄3. Stringpath=File.getAbsoultePath();/絕對路經(jīng)4. Stringpath=File.getPath();/相對路經(jīng)5. File.cr

12、eateNewFile();/建立文件6. File.mkDir();/建立文件夾7. File.isDirectory();/判斷是文件或文件夾8. Filefiles=File.listFiles();/列出文件夾下的所有文件和文件夾名9. File.renameTo(dest);/修改文件夾和文件名10. File.delete();/刪除文件夾或文件六、使用RandomAccessFile進(jìn)行文件的讀寫:RandomAccessFile的使用方法比較靈活,功能也比較多,可以使用類似seek的方式可以跳轉(zhuǎn)到文件的任意位置,從文件指示器當(dāng)前位置開始讀寫。它有兩種構(gòu)造方法new Random

13、AccessFile(f,rw);/讀寫方式new RandomAccessFile(f,r);/只讀方式使用事例:javaview plaincopy1. /*2. *程序功能:演示了RandomAccessFile類的操作,同時(shí)實(shí)現(xiàn)了一個(gè)文件復(fù)制操作。3. */4. 5. importjava.io.*;6. 7. publicclassRandomAccessFileDemo8. publicstaticvoidmain(Stringargs)throwsException9. RandomAccessFilefile=newRandomAccessFile(file,rw);10. /

14、以下向file文件中寫數(shù)據(jù)11. file.writeInt(20);/占4個(gè)字節(jié)12. file.writeDouble(8.236598);/占8個(gè)字節(jié)13. file.writeUTF(這是一個(gè)UTF字符串);/這個(gè)長度寫在當(dāng)前文件指針的前兩個(gè)字節(jié)處,可用readShort()讀取14. file.writeBoolean(true);/占1個(gè)字節(jié)15. file.writeShort(395);/占2個(gè)字節(jié)16. file.writeLong(2325451l);/占8個(gè)字節(jié)17. file.writeUTF(又是一個(gè)UTF字符串);18. file.writeFloat(35.5f)

15、;/占4個(gè)字節(jié)19. file.writeChar(a);/占2個(gè)字節(jié)20. 21. file.seek(0);/把文件指針位置設(shè)置到文件起始處22. 23. /以下從file文件中讀數(shù)據(jù),要注意文件指針的位置24. System.out.println(從file文件指定位置讀數(shù)據(jù));25. System.out.println(file.readInt();26. System.out.println(file.readDouble();27. System.out.println(file.readUTF();28. 29. file.skipBytes(3);/將文件指針跳過3個(gè)字節(jié),

16、本例中即跳過了一個(gè)boolean值和short值。30. System.out.println(file.readLong();31. 32. file.skipBytes(file.readShort();/跳過文件中“又是一個(gè)UTF字符串”所占字節(jié),注意readShort()方法會(huì)移動(dòng)文件指針,所以不用加2。33. System.out.println(file.readFloat();34. 35. /以下演示文件復(fù)制操作36. System.out.println(文件復(fù)制(從file到fileCopy));37. file.seek(0);38. RandomAccessFilefi

17、leCopy=newRandomAccessFile(fileCopy,rw);39. intlen=(int)file.length();/取得文件長度(字節(jié)數(shù))40. byteb=newbytelen;41. file.readFully(b);42. fileCopy.write(b);43. System.out.println(復(fù)制完成!);44. 45. 七、讀取資源文件時(shí)能否實(shí)現(xiàn)類似于seek的方式可以跳轉(zhuǎn)到文件的任意位置,從指定的位置開始讀取指定的字節(jié)數(shù)呢?答案是可以的。在FileInputStream和InputStream中都有下面的函數(shù):javaview plaincop

18、y1. publiclongskip(longbyteCount);/從數(shù)據(jù)流中跳過n個(gè)字節(jié)2. publicintread(bytebuffer,intoffset,intlength);/從數(shù)據(jù)流中讀取length數(shù)據(jù)存在buffer的offset開始的位置。offset是相對于buffer的開始位置的,不是數(shù)據(jù)流。可以使用這兩個(gè)函數(shù)來實(shí)現(xiàn)類似于seek的操作,請看下面的測試代碼:javaview plaincopy1. /其中read_raw是一個(gè)txt文件,存放在raw目錄下。2. /read_raw.txt文件的內(nèi)容是:ABCDEFGHIJKLMNOPQRST3. publicStr

19、inggetRawString()throwsIOException4. 5. Stringstr=null;6. 7. InputStreamin=getResources().openRawResource(R.raw.read_raw);8. 9. intlength=in.available();10. bytebuffer=newbytelength;11. 12. in.skip(2);/跳過兩個(gè)字節(jié)13. in.read(buffer,0,3);/讀三個(gè)字節(jié)14. 15. in.skip(3);/跳過三個(gè)字節(jié)16. in.read(buffer,0,3);/讀三個(gè)字節(jié)17. 18

20、. /最后str=IJK19. str=EncodingUtils.getString(buffer,BIG5);20. 21. 22. in.close();23. 24. returnstr;25. 從上面的實(shí)例可以看出skip函數(shù)有點(diǎn)類似于C語言中的seek操作,但它們之間有些不同。需要注意的是:1、skip函數(shù)始終是從當(dāng)前位置開始跳的。在實(shí)際應(yīng)用當(dāng)中還要再判斷一下該函數(shù)的返回值。2、read函數(shù)也始終是當(dāng)前位置開始讀的。3、另外,還可以使用reset函數(shù)將文件的當(dāng)前位置重置為0,也就是文件的開始位置。如何得到文件的當(dāng)前位置?我沒有找到相關(guān)的函數(shù)和方法,不知道怎么樣才能得到文件的當(dāng)前位置

21、,貌似它也并不是太重要。八、如何從FileInputStream中得到InputStream?javaview plaincopy1. publicStringreadFileData(StringfileName)throwsIOException2. Stringres=;3. try4. FileInputStreamfin=newFileInputStream(fileName);5. InputStreamin=newBufferedInputStream(fin);6. 7. .8. 9. 10. catch(Exceptione)11. e.printStackTrace();12. 13. 14. 九、APK資源文件的大小不能超過1M,如果超過了怎么辦?我們可以將這個(gè)數(shù)據(jù)再復(fù)制到data目錄下,然后再使用。復(fù)制數(shù)據(jù)的代碼如下:javaview plaincopy1. publicbooleanassetsCopyData(StringstrAssetsFilePath,StringstrDesFilePath)2. booleanbIsSuc=true;3. InputStreaminputStream=null;4. OutputSt

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論