v客学院安卓开发文件操作

上传人:xiao****1972 文档编号:84828599 上传时间:2019-03-05 格式:DOC 页数:9 大小:131.50KB
返回 下载 相关 举报
v客学院安卓开发文件操作_第1页
第1页 / 共9页
v客学院安卓开发文件操作_第2页
第2页 / 共9页
v客学院安卓开发文件操作_第3页
第3页 / 共9页
v客学院安卓开发文件操作_第4页
第4页 / 共9页
v客学院安卓开发文件操作_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《v客学院安卓开发文件操作》由会员分享,可在线阅读,更多相关《v客学院安卓开发文件操作(9页珍藏版)》请在金锄头文库上搜索。

1、安卓文件操作在android中的文件放在不同位置,它们的读取方式也有一些不同。本文对android中对资源文件的读取、数据区文件的读取、SD卡文件的读取及RandomAccessFile的方式和方法进行了整理。供参考。一、资源文件的读取: 1) 从resource的raw中读取文件数据:1. Stringres=;2. try3. 4. /得到资源中的Raw数据流5. InputStreamin=getResources().openRawResource(R.raw.test);6. 7. /得到数据的大小8. intlength=in.available();9. 10. bytebuff

2、er=newbytelength;11. 12. /读取数据13. in.read(buffer);14. 15. /依test.txt的编码类型选择合适的编码,如果不调整会乱码16. res=EncodingUtils.getString(buffer,BIG5);17. 18. /关闭19. in.close();20. 21. catch(Exceptione)22. e.printStackTrace();23. 2) 从resource的asset中读取文件数据1. StringfileName=test.txt;/文件名字2. Stringres=;3. try4. 5. /得到资

3、源中的asset数据流6. InputStreamin=getResources().getAssets().open(fileName);7. 8. intlength=in.available();9. bytebuffer=newbytelength;10. 11. in.read(buffer);12. in.close();13. res=EncodingUtils.getString(buffer,UTF-8);14. 15. catch(Exceptione)16. 17. e.printStackTrace();18. 19. 二、读写/data/data/目录上的文件:1.

4、/写数据2. publicvoidwriteFile(StringfileName,Stringwritestr)throwsIOException3. try4. 5. FileOutputStreamfout=openFileOutput(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. /读数据2

5、0. publicStringreadFile(StringfileName)throwsIOException21. Stringres=;22. try23. FileInputStreamfin=openFileInput(fileName);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.pr

6、intStackTrace();32. 33. returnres;34. 35. 三、读写SD卡中的文件。也就是/mnt/sdcard/目录下面的文件 :1. /写数据到SD中的文件2. publicvoidwriteFileSdcardFile(StringfileName,Stringwrite_str)throwsIOException3. try4. 5. FileOutputStreamfout=newFileOutputStream(fileName);6. bytebytes=write_str.getBytes();7. 8. fout.write(bytes);9. fou

7、t.close();10. 11. 12. catch(Exceptione)13. e.printStackTrace();14. 15. 16. 17. 18. /读SD中的文件19. publicStringreadFileSdcardFile(StringfileName)throwsIOException20. Stringres=;21. try22. FileInputStreamfin=newFileInputStream(fileName);23. 24. intlength=fin.available();25. 26. bytebuffer=newbytelength;2

8、7. fin.read(buffer);28. 29. res=EncodingUtils.getString(buffer,UTF-8);30. 31. fin.close();32. 33. 34. catch(Exceptione)35. e.printStackTrace();36. 37. returnres;38. 四、使用File类进行文件的读写:1. /读文件2. publicStringreadSDFile(StringfileName)throwsIOException3. 4. Filefile=newFile(fileName);5. 6. FileInputStrea

9、mfis=newFileInputStream(file);7. 8. intlength=fis.available();9. 10. bytebuffer=newbytelength;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. Fil

10、efile=newFile(fileName);23. 24. FileOutputStreamfos=newFileOutputStream(file);25. 26. bytebytes=write_str.getBytes();27. 28. fos.write(bytes);29. 30. fos.close();31. 五、另外,File类还有下面一些常用的操作:1. StringName=File.getName();/获得文件或文件夹的名称:2. StringparentPath=File.getParent();/获得文件或文件夹的父目录3. Stringpath=File.g

11、etAbsoultePath();/绝对路经4. Stringpath=File.getPath();/相对路经5. File.createNewFile();/建立文件6. File.mkDir();/建立文件夹7. File.isDirectory();/判断是文件或文件夹8. Filefiles=File.listFiles();/列出文件夹下的所有文件和文件夹名9. File.renameTo(dest);/修改文件夹和文件名10. File.delete();/删除文件夹或文件六、使用RandomAccessFile进行文件的读写:RandomAccessFile的使用方法比较灵活,功能也比较多,可以使用类似seek的方式可以跳转到文件的任意位置,从文件指示器当前位置开

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号