android文件读写

android文件的读写主要分为两个方面,一个是将内容写入本应用的data文件夹中,另一个是将内容写入到sdcard中。两者都使用I/O流的读写技术。

下面具体具体介绍这两方面的内容:

一。将内容写入本应用中:

/**
	 * @description:将内容保存到内置存储中
	 * @author:Administrator
	 * @return:boolean
	 * @param fileName
	 *            文件名
	 * @param fileContent
	 *            文件内容
	 * @param context
	 *            上下文对象
	 * @return
	 */

	public static boolean fileSaveApp(String fileName, String fileContent,
			Context context) {
		try {
			// 用android提供的输出流来将内容写入到文件中,注意mode的用途
			FileOutputStream fos = context.openFileOutput(fileName,
					context.MODE_APPEND);
			fos.write(fileContent.getBytes());
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}


注意android的上下文对象context在这个方法中起的的作用(可以用它来打开一个输出流,或者打开一个输入流),还有文件的写入模式。这里用的是文件的追加模式。数据时只可被本应用操作的。

 

二。将数据写入sdcard中

 

/**
	 * @description:将内容写入到sdcard的文件当中
	 * @author:Administrator
	 * @return:boolean
	 * @param fileName
	 *            文件名
	 * @param fileContent
	 *            文件内容
	 * @param path
	 *            文件路径
	 * @return
	 */

	public static boolean fileSave(String fileName, String fileContent,
			File path) {
		File file = new File(path, fileName);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		FileOutputStream fos = null;
		int count = 0;
		try {
			fos = new FileOutputStream(file);
			count = fileContent.getBytes().length;
			fos.write(fileContent.getBytes(), 0, count);
			fos.close();
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}


注意这个方法中的path参数指的是sdcard的路径:=Environment.getExternalStorageDirectory();

注意写入数据的时候需要判断sdcard是否存在是否可写:Environment.getExternalStorageState().equals(
       Environment.MEDIA_MOUNTED);

最后需要注意如果要向sdcard写入数据必须在manifest.xml加入权限: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />第一个权限是指对sdcard可写,第二个权限是指可以在sdcard中建立和删除文件。

 

最后将从应用的data中读取数据的方法也写进来:

 

/**
	 * @description:读取本应用data文件夹中文件的内容(如果要读取sdcard中的文件时一定要去判断sdcard是否存在,并且是可读的)
	 * @author:Administrator
	 * @return:String 文件的内容
	 * @param fileName
	 *            文件名
	 * @param context
	 *            上下文对象
	 * @return
	 */

	public static String readFile(String fileName, Context context) {
		String str = "";
		
			try {
				FileInputStream fis = context.openFileInput(fileName);
				ByteArrayOutputStream bos = new ByteArrayOutputStream();
				byte[] buffer = new byte[1024];
				int len = 0;
				while ((len = fis.read(buffer)) != -1) {
					// 用字节输出流将读到的内容写入到内存中
					bos.write(buffer, 0, len);
				}
				// 将输出流中的数据转换为String
				str = new String(bos.toByteArray(), "utf-8");
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		return str;
	}


这里就不再介绍从sdcard中读取文件内容的方法了。

 

 

这里将android本应用的文件存储的4中方式列举出来:可以参考 http://www.cnblogs.com/jqyp/archive/2010/09/13/1825249.html

Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加到原文件中。可以使用Context.MODE_APPEND
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。
Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件。
MODE_WORLD_READABLE:表示当前文件可以被其他应用读取; MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。
如果希望文件被其他应用读和写,可以传入:
openFileOutput("itcast.txt", Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android开发中,文件操作是非常常见的,比如读取或写入文本、图片或视频等文件。本文将简要介绍在Android中实现文件读写的一些基本知识和技巧。 1. 读取文件 在Android中,读取文件通常有两种常用的方法:使用FileInputStream和BufferedReader。如下所示: String filePath = "your/file/path"; // 使用FileInputStream读取 FileInputStream inputStream = new FileInputStream(filePath); byte[] buffer = new byte[1024]; int length = 0; while ((length = inputStream.read(buffer)) > 0) { String content = new String(buffer, 0, length); Log.i("File", "Content: " + content); } inputStream.close(); // 使用BufferedReader读取 FileReader fileReader = new FileReader(filePath); BufferedReader bufferedReader = new BufferedReader(fileReader); String line; while ((line = bufferedReader.readLine()) != null) { Log.i("File", "Line: " + line); } bufferedReader.close(); 2. 写入文件 如果需要向文件中写入内容,我们可以使用FileOutputStream和BufferedWriter来实现。 String filePath = "your/file/path"; // 使用FileOutputStream写入 FileOutputStream outputStream = new FileOutputStream(filePath); String content = "Hello World!"; outputStream.write(content.getBytes()); outputStream.close(); // 使用BufferedWriter写入 FileWriter fileWriter = new FileWriter(filePath); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); String line = "Hello World!"; bufferedWriter.write(line); bufferedWriter.flush(); bufferedWriter.close(); 需要注意的是,在执行文件写入操作时,需要先授予应用程序写入文件的权限。可以在AndroidManifest.xml中添加WRITE_EXTERNAL_STORAGE权限。 总结 以上是Android文件读写的基本内容。在实际开发中,还需要注意文件路径的获取、文件不存在的判断等问题。另外,需要注意写入的权限授权和向用户说明应用程序写入的具体目的和内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值