Android入门笔记 - 数据存储 - 文件

来看看第二个数据存储方式,那就是文件。在android开发中有自己的文件操作,当然也可以使用java.io 的文件操作。我们来看看下面几个例子:

今天就不贴整的代码了,分开看例子:

1. 读取assets/ 目录下的文件:

		// 1. 读写 assets 目录下的文件
		try {
			InputStream is = getResources().getAssets().open(
					"sounds/Dreamer.mp3");
			int length = is.available();
			Log.e(TAG, "length=" + length);
			byte[] buffer = new byte[length];
			is.read(buffer);
			is.close();

			File file = new File(filedir, "Dreamer(copy).mp3");
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(buffer);
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {

		}
主要用到了 getResources().getAssets().open("sounds/Dreamer.mp3"); 这就是得到 assets/ sounds/ Dreamer.mp3 的输入流,然后用read()方法可以将流读到byte[] 数组中。

这里读入文件后,我们将文件写出到 SDcard中, filedir其实是路径:mnt/sdcard/ 。我们来看一看具体效果:

原文件:

拷贝文件:



2. 读取和写入 data/data/ 包名/ 目录下的文件

		// 3.读取和写入 data/data/包名/ 目录下的文件
		try {
			FileOutputStream fileoutputstream = this.openFileOutput("test.html", MODE_PRIVATE);
			String words = "你好 Hello";
			byte[] buffer = words.getBytes();
			fileoutputstream.write(buffer);
			fileoutputstream.close();

			FileInputStream fileinputstream = this.openFileInput("test.html");
			int length = fileinputstream.available();
			byte[] bufferRead = new byte[length];
			fileinputstream.read(bufferRead);
			String wordsRead = EncodingUtils.getString(bufferRead, "UTF-8");
			Log.e(TAG, "wordsRead:" + wordsRead);
			fileinputstream.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {

		}
像访问 assets/ 目录一样, 要访问包名下的文件也是一样,需要使用特殊的方法,这里使用的是 activity.openFileOutput(),  写出成功后我们来看一下文件目录:

系统自动在包名下创建 files/ 目录来存放文件,这里需要注意,在使用 openFileOutput()的时候,就不能再添加路径了,因为系统自动处理放在files/ 下面,不想使用assets/ 的时候,还可以添加路径, 那么就是在 assets/ 路径/ 下面创建创建文件。

读取也是一样,需要使用  activity.openFileInput() 才能打开。


3.  读取和写入 SDcard 目录下的文件, SDcard的目录为: mnt/sdcard 

SDcard的目录获取方式:

		File filedir = Environment.getExternalStorageDirectory();
		String filepath = filedir.getPath();
文件操作:

		try {
			FileOutputStream fos = new FileOutputStream(filepath
					+ "/testSD.txt");
			String words = "boy 男孩";
			byte[] buffer = words.getBytes();
			fos.write(buffer);
			fos.close();

			FileInputStream fis = new FileInputStream(filepath + "/testSD.txt");
			byte[] bufferRead = new byte[fis.available()];
			fis.read(bufferRead);
			String wordsRead = EncodingUtils.getString(bufferRead, "UTF-8");
			fis.close();
			Log.e(TAG, "wordsRead:" + wordsRead);
		} catch (IOException e) {
			e.printStackTrace();
		}

4. 使用File类进行文件读写:

		try {
			File f = new File(filepath, "testSD.txt");
			FileOutputStream fos = new FileOutputStream(f);
			String words = "girl 女孩";
			byte[] buffer = words.getBytes();
			fos.write(buffer);
			fos.close();

			File fread = new File(filepath, "testSD.txt");
			FileInputStream fis = new FileInputStream(fread);
			byte[] bufferRead = new byte[fis.available()];
			fis.read(bufferRead);
			String wordsRead = EncodingUtils.getString(bufferRead, "UTF-8");
			fis.close();
			Log.e(TAG, "wordsRead:" + wordsRead);
		} catch (IOException e) {
			e.printStackTrace();
		}

这里单独将File提出来,是因为File类很使用,java中不管文件还是路径都可以看成File(应该来源于Linux),操作起来很方便。File类还提供很多有用的函数:

String Name = File.getName();  //获得文件或文件夹的名称:
String parentPath = File.getParent();  获得文件或文件夹的父目录
String path = File.getAbsoultePath(); //绝对路经
String path = File.getPath(); //相对路经
File.createNewFile(); //建立文件 
File.mkDir(); //建立文件夹 
File.isDirectory(); //判断是文件或文件夹
File[] files = File.listFiles();  //列出文件夹下的所有文件和文件夹名
File.renameTo(dest);  //修改文件夹和文件名
File.delete();  //删除文件夹或文件

最后还有文件的随机读写,使用 RandomAccessFile, 这个就不细讲了,大家可以去参看其它。

总的来说在android使用文件操作和java.io 是差不多的,看看就懂。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值