首页 > 移动平台 > 详细

Android数据储存之File

时间:2016-01-27 21:11:50      阅读:262      评论:0      收藏:0      [点我收藏+]

openFileOutStream 和 openFileInStream

FileInputStream fileInputStream = openFileInput(name);  打开应用下文件名称问name的输入流;

获取应用下某个文件的内容:

/**
	 * 读
	 * @return
	 */
	public String read(){
		try {
			//打开输入流
			FileInputStream fileInputStream = openFileInput(name);
			//创建byte数组
			byte[] buffer = new byte[1034];
			int i = 0;
			//创建StringBuilder 对象
			StringBuilder builder = new StringBuilder();
			//循环读取fileInputStream中的字节
			while ((i =fileInputStream.read(buffer)) > 0) {
				builder.append(new String(buffer, 0, i));
			}
			//关闭输入流
			fileInputStream.close();
			//返回输入流中的字符串
			return builder.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

 FileOutputStream fileOutputStream = openFileOutput(name, MODE_PRIVATE);打开本应用下名称为name的输出流

第一个参数为文件名称;

第二个参数为写入模式;

写入模式常用有四种:

MODE_ORIVATE  该文件只能被当前程序读写

MODE_APPEND   已追加的方式打开文件,程序可以向文件中追加内容

MODE_WORLD_READABLE 该文件的内容可以被其他程序读取

MODE_WORLD_WEITEABLE 该文件的内容可以被其他程序读写

示例:

/**
	 * 写
	 * @param str 文件中的字符串(内容)
	 */
	public void write(String str){
		try {
			//获取输出流对象,已追加的方式打开输出流
			FileOutputStream fileOutputStream = openFileOutput(name, MODE_PRIVATE);
			//将输出流封装成PrintStream对象
			PrintStream printStream = new PrintStream(fileOutputStream);
			//输出写入内容
			printStream.print(str);
			//关闭输出流
			printStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

 Context提供访问应用程序数据文件夹方法如下:

getDri(String name,int mode)  在应用程序数据文件加下创建或打开以name为名称的子目录

File getFileDri()    获取应用程序的数据文件夹的绝对路径

String[] fileList() 返回应用程序文件夹下全部文件

deleteFile(String name) 删除名称为name的数据文件

 

Android数据储存之File

原文:http://www.cnblogs.com/shiguotao-com/p/5164368.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!