Android文件操作个人技术总结(如有误,请轻喷)
Android文件操作是建立在Java IO流的基础上,以下是Java IO流结构草图(网上基本都搜得到这个图):
通过上图,我们能清楚的认识到Java IO流的结构分布,其中各个流的详细用法这里不做过多的解释,我们直接切入正题,通过下列代码讲解安卓文件操作:
一、Android常用存放文件路径及其调用方法
1、从resource中读取文件数据
打开resource下的raw文件夹中的文件(注:raw文件夹一般需要自己建立):
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- /**
- * 上下文.getResources().openRawResource(R.raw.aa);
- * 注:在Activity中上下文可省略*/
- InputStream inputStream=this.getResources().openRawResource(R.raw.aa);
- }
打开resource下的assets文件夹中的文件:
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- /**上下文.getResources().getAssets().open("cc.text");
- * 注:在Activity中上下文可省略
- * 打开资源目录下的assets文件目录
- * 注:当前目录只能读不能写
- * */
- try {
- InputStream inputStream=this.getResources().getAssets().open("cc.text");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
2、读写应用程序名目录(/data/data/应用程序名)上的文件
- //文件写入
- public void writeFile(){
- String info="文件写入";
- try {
- //声明文件输出流写入到应用目录上的文件,其中第二个参数是文件操作时的权限,注:在普通类中需上下文调出
- FileOutputStream fileOutputStream=openFileOutput("ceshi.txt", MODE_PRIVATE);
- byte[] bytes=info.getBytes();//String类型转成byte类型
- fileOutputStream.write(bytes);//写入文件
- fileOutputStream.close();//关闭文件输出流
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- //文件读取
- public void readFile(){
- try {
- FileInputStream fileInputStream=openFileInput("ceshi.txt");//声明文件输入流读取应用目录上的文件
- int lenth=fileInputStream.available();//获取文件长度
- byte[] bytes=new byte[lenth];//存放读取出的文件内容
- fileInputStream.read(bytes);//读取文件
- String result=EncodingUtils.getString(bytes, "UTF-8");//byte类型转成String类型并转码
- fileInputStream.close();//关闭文件输入流
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
3、读写SD卡中的文件
只需修改上述代码文件输入输出流声明部分的代码
- /**
- * 文件写入
- * Environment.getExternalStorageDirectory()是android4.0以后的的SD卡路径
- * */
- File file=new File(Environment.getExternalStorageDirectory()+"/ceshi.txt");
- FileOutputStream fileOutputStream=new FileOutputStream(file);
- /**
- * 文件读取
- * Environment.getExternalStorageDirectory()是android4.0以后的的SD卡路径
- * */
- File file=new File(Environment.getExternalStorageDirectory()+"/ceshi.txt");
- FileInputStream fileInputStream=new FileInputStream(file);
只需修改文件输入输出实例化的文件路径
- //文件写入
- File file=new File("/ceshi.txt");
- FileOutputStream fileOutputStream=new FileOutputStream(file);
- //文件读取
- File file=new File("/ceshi.txt");
- FileInputStream fileInputStream=new FileInputStream(file);
- public String readFile(){
- String string="";
- try {
- File file=new File(Environment.getExternalStorageDirectory()+"/ceshi.txt");
- FileInputStream fileInputStream=new FileInputStream(file);
- InputStreamReader inputStreamReader=new InputStreamReader(fileInputStream);
- BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
- String line="";
- while((line=bufferedReader.readLine())!=null){
- string+=line+"\n";
- }
- bufferedReader.close();
- inputStreamReader.close();
- fileInputStream.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return string;
- }
- public String readFile(){
- String string="";
- try {
- File file=new File(Environment.getExternalStorageDirectory()+"/ceshi.txt");
- FileInputStream fileInputStream=new FileInputStream(file);
- int length=fileInputStream.available();
- ByteArrayOutputStream outputStream=new ByteArrayOutputStream();//建立缓存流存放文件内容
- byte[] bytes=new byte[length];
- int len=0;
- if(fileInputStream!=null){
- while((len=fileInputStream.read(bytes))!=-1){
- outputStream.write(bytes, 0, len);
- }
- }
- //string=EncodingUtils.getString(outputStream.toByteArray(), "UTF-8");//转码
- string=outputStream.toString();
- outputStream.close();
- fileInputStream.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return string;
- }