Android中的多种文件读写操作方法

这里补充一下权限:

  1. <!-- 往SDCard写入数据权限 --> 
  2. <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
  3. <!-- 在SDCard中创建与删除文件权限 --> 
  4. <uses-permissionandroid:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> 

 

  1. package com.ppmeet; 
  2.  
  3. import java.io.File; 
  4. import java.io.FileInputStream; 
  5. import java.io.FileOutputStream; 
  6. import java.io.IOException; 
  7. import java.io.InputStream; 
  8.  
  9. import org.apache.http.util.EncodingUtils; 
  10.  
  11. import android.content.Context; 
  12.  
  13. /**
  14. * class name:FileService<BR>
  15. * class description:android文件的一些读取操作<BR>
  16. * PS: <BR>
  17. *
  18. * @version 1.00 2010/10/21
  19. * @author CODYY)peijiangping
  20. */ 
  21. public class FileService { 
  22.     private Context context; 
  23.  
  24.     public FileService(Context c) { 
  25.         this.context = c; 
  26.     } 
  27.  
  28.     // 读取sd中的文件 
  29.     public String readSDCardFile(String path)throws IOException { 
  30.         File file = new File(path); 
  31.         FileInputStream fis = new FileInputStream(file); 
  32.         String result = streamRead(fis); 
  33.         return result; 
  34.     } 
  35.  
  36.     // 在res目录下建立一个raw资源文件夹,这里的文件只能读不能写入。。。 
  37.     public String readRawFile(int fileId)throws IOException { 
  38.         // 取得输入流 
  39.         InputStream is = context.getResources().openRawResource(fileId); 
  40.         String result = streamRead(is);// 返回一个字符串 
  41.         return result; 
  42.     } 
  43.  
  44.     private String streamRead(InputStream is)throws IOException { 
  45.         int buffersize = is.available();// 取得输入流的字节长度 
  46.         byte buffer[] = newbyte[buffersize]; 
  47.         is.read(buffer);// 将数据读入数组 
  48.         is.close();// 读取完毕后要关闭流。 
  49.         String result = EncodingUtils.getString(buffer, "UTF-8");// 设置取得的数据编码,防止乱码 
  50.         return result; 
  51.     } 
  52.  
  53.     // 在assets文件夹下的文件,同样是只能读取不能写入 
  54.     public String readAssetsFile(String filename)throws IOException { 
  55.         // 取得输入流 
  56.         InputStream is = context.getResources().getAssets().open(filename); 
  57.         String result = streamRead(is);// 返回一个字符串 
  58.         return result; 
  59.     } 
  60.  
  61.     // 往sd卡中写入文件 
  62.     public void writeSDCardFile(String path,byte[] buffer)throws IOException { 
  63.         File file = new File(path); 
  64.         FileOutputStream fos = new FileOutputStream(file); 
  65.         fos.write(buffer);// 写入buffer数组。如果想写入一些简单的字符,可以将String.getBytes()再写入文件; 
  66.         fos.close(); 
  67.     } 
  68.  
  69.     // 将文件写入应用的data/data的files目录下 
  70.     public void writeDateFile(String fileName,byte[] buffer)throws Exception { 
  71.         byte[] buf = fileName.getBytes("iso8859-1"); 
  72.         fileName = new String(buf, "utf-8"); 
  73.         // Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加到原文件中。可以使用Context.MODE_APPEND 
  74.         // Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。 
  75.         // Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件。 
  76.         // MODE_WORLD_READABLE:表示当前文件可以被其他应用读取;MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。 
  77.         // 如果希望文件被其他应用读和写,可以传入: 
  78.         // openFileOutput("output.txt", Context.MODE_WORLD_READABLE + 
  79.         // Context.MODE_WORLD_WRITEABLE); 
  80.         FileOutputStream fos = context.openFileOutput(fileName, 
  81.                 Context.MODE_APPEND);// 添加在文件后面 
  82.         fos.write(buffer); 
  83.         fos.close(); 
  84.     } 
  85.  
  86.     // 读取应用的data/data的files目录下文件数据 
  87.     public String readDateFile(String fileName)throws Exception { 
  88.         FileInputStream fis = context.openFileInput(fileName); 
  89.         String result = streamRead(fis);// 返回一个字符串 
  90.         return result; 
  91.     } 
package com.ppmeet;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.util.EncodingUtils;

import android.content.Context;

/**
 * class name:FileService<BR>
 * class description:android文件的一些读取操作<BR>
 * PS: <BR>
 * 
 * @version 1.00 2010/10/21
 * @author CODYY)peijiangping
 */
public class FileService {
	private Context context;

	public FileService(Context c) {
		this.context = c;
	}

	// 读取sd中的文件
	public String readSDCardFile(String path) throws IOException {
		File file = new File(path);
		FileInputStream fis = new FileInputStream(file);
		String result = streamRead(fis);
		return result;
	}

	// 在res目录下建立一个raw资源文件夹,这里的文件只能读不能写入。。。
	public String readRawFile(int fileId) throws IOException {
		// 取得输入流
		InputStream is = context.getResources().openRawResource(fileId);
		String result = streamRead(is);// 返回一个字符串
		return result;
	}

	private String streamRead(InputStream is) throws IOException {
		int buffersize = is.available();// 取得输入流的字节长度
		byte buffer[] = new byte[buffersize];
		is.read(buffer);// 将数据读入数组
		is.close();// 读取完毕后要关闭流。
		String result = EncodingUtils.getString(buffer, "UTF-8");// 设置取得的数据编码,防止乱码
		return result;
	}

	// 在assets文件夹下的文件,同样是只能读取不能写入
	public String readAssetsFile(String filename) throws IOException {
		// 取得输入流
		InputStream is = context.getResources().getAssets().open(filename);
		String result = streamRead(is);// 返回一个字符串
		return result;
	}

	// 往sd卡中写入文件
	public void writeSDCardFile(String path, byte[] buffer) throws IOException {
		File file = new File(path);
		FileOutputStream fos = new FileOutputStream(file);
		fos.write(buffer);// 写入buffer数组。如果想写入一些简单的字符,可以将String.getBytes()再写入文件;
		fos.close();
	}

	// 将文件写入应用的data/data的files目录下
	public void writeDateFile(String fileName, byte[] buffer) throws Exception {
		byte[] buf = fileName.getBytes("iso8859-1");
		fileName = new String(buf, "utf-8");
		// Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加到原文件中。可以使用Context.MODE_APPEND
		// Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。
		// Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件。
		// MODE_WORLD_READABLE:表示当前文件可以被其他应用读取;MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。
		// 如果希望文件被其他应用读和写,可以传入:
		// openFileOutput("output.txt", Context.MODE_WORLD_READABLE +
		// Context.MODE_WORLD_WRITEABLE);
		FileOutputStream fos = context.openFileOutput(fileName,
				Context.MODE_APPEND);// 添加在文件后面
		fos.write(buffer);
		fos.close();
	}

	// 读取应用的data/data的files目录下文件数据
	public String readDateFile(String fileName) throws Exception {
		FileInputStream fis = context.openFileInput(fileName);
		String result = streamRead(fis);// 返回一个字符串
		return result;
	}
}

这里提供一个我写好的封装类。

解释都放注释里面了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio,有多种方式可以进行文件操作。其包括应用程序下的文件、assets下的文件取、raw下的文件取以及SD卡下的文件。 首先,对于应用程序下的文件Android文件存储在应用的内部存储空间,在这个空间,你可以使用openFileOutput()方法创建一个输出流来文件。例如,可以使用以下代码创建一个文件入内容: ```java public void WriteSysFile(Context context, String filename) { try { FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE); OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); osw.write("随便插入点啥"); osw.flush(); fos.flush(); osw.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } ``` 这个方法将在应用的内部存储空间创建一个名为"appfile.txt"的文件,并将"随便插入点啥"入到该文件。 其次,如果需要取assets文件夹下的文件,可以使用context.getResources().getAssets().open()方法来获取一个InputStream对象,然后再使用这个对象进行文件操作。 另外,如果想要取res/raw文件夹下的文件,可以使用context.getResources().openRawResource()方法来获取一个InputStream对象,并通过该对象进行文件取。 最后,如果要进行SD卡下的文件操作,可以使用File类来指定文件的路径,并使用FileOutputStream和FileInputStream来进行文件操作。 需要注意的是,在进行SD卡下的文件操作时,需要确保已经获取了相应的权限。 总结起来,Android Studio提供了多种方式用于文件操作,包括应用程序下的文件、assets下的文件取、raw下的文件取以及SD卡下的文件。具体选择哪种方式取决于你的需求和文件的位置。 引用自:本篇介绍android开发4文件方式。 引用自:在as里提供了DeviceFileExplorer查看应用程序的目录,具体路径是/data/data/应用程序包名。 引用自:首先添加raw文件夹,在res文件夹上右键添加。这里的文件最后会被打包到apk,同时会被编译到R.java文件,访问的时候直接使用资源ID。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值