android文件管理

来源:互联网 发布:下载打字的软件 编辑:程序博客网 时间:2024/05/16 17:35

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 blocke.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 blocke.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 blocke.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);
4、读写其他路径的文件

只需修改文件输入输出实例化的文件路径

//文件写入File file=new File("/ceshi.txt");FileOutputStream fileOutputStream=new FileOutputStream(file);
//文件读取File file=new File("/ceshi.txt");FileInputStream fileInputStream=new FileInputStream(file);
5、其他方法的文件操作

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;}



1 0