android资源文件io,以及内存外存的io

package com.example.combat_12_17_io1;


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


import org.apache.http.util.EncodingUtils;


import android.app.Activity;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;


public class MainActivity extends Activity {
	
	private static final String ACTIVITY_TAG="LogDemo";  
	private FileInputStream fis;
	private String Buffer;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//用封装好的类取得存放在raw目录下资源文件的string 或者 io;;
		Usingrawresources us = new Usingrawresources(R.raw.test,this);
		Buffer = us.getresourcesbystring();
		Log.i(ACTIVITY_TAG, "OK");
		try {
			fis = us.getresourcesInputbystring();
			Log.i(ACTIVITY_TAG,"ok");
			
		} catch (Exception e) {
			e.printStackTrace();
		
		}
		
	}
}


class Usingrawresources{
	
	private String res ;
	private int resources;
	private Context context;
	//用来得到resource资源文件的地址;
	public Usingrawresources(int resourcesname,Context context){
		resources = resourcesname;
		this.context = context;
	}
	
	/*
	 * 读取资源文件并且返回string类型的 结果;
	 */
	public String getresourcesbystring(){
		
		try{
			//打开资源文件的inputstream;
			InputStream in = context.getResources().openRawResource(resources); 
			Integer length = in.available();
			
			//将资源文件所有的内容都写入buffer中;
			byte[] buffer = new byte[length];
			in.read(buffer);         
			
			//将字节数组中的内容都写入String中并用utf-8编码
			res = EncodingUtils.getString(buffer, "UTF-8");
			//System.out.println(res);
			in.close();
			
		   }catch(Exception e){ 
		      e.printStackTrace();
		   } 
		return res;
	}
	
	private FileInputStream fis;
	
	
	/*
	 * 读取资源文件并且 返回一个inputStream供给读取文件;
	 */
	public FileInputStream getresourcesInputbystring(){
		//打开纸团文件得到一个assetFileDescripetor 
		try{
			AssetFileDescriptor afd = context.getResources().openRawResourceFd(resources);
			//用AssetfileDescriptor进行打开其输入流的操作
			fis = afd.createInputStream();
		}catch(Exception e){
			System.out.println("fail");
		}
		return fis;
	}
	
}


class Usingassetresources{
	private String resources;
	private String res;
	private Context context;
	public Usingassetresources(String resourcesname,Context context){
		resources = resourcesname;
		this.context = context;
	}
	
	public String getresourcesbystring() {
		
		try{			
			//就和raw文件读取有一点不同,这里换了种办法去的他的inputStream;
			InputStream in = context.getResources().getAssets().open(resources);
			int length = in.available();         
			
			byte [] buffer = new byte[length];        
			in.read(buffer);            			
			
			res = EncodingUtils.getString(buffer, "UTF-8");
		
		}catch(Exception e){ 
			e.printStackTrace();         
		}
		return res;
	}
	
}




/*
 * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 *
 * 在SD卡上创建文件及文件夹
 */
class writetoSDcard{
	private Context context;
	private String filename;
	private String SDpath;// SD卡的路径 
	private boolean hasSD = false;//SD卡是否存在。 
	private String FILESPATH;// 当前程序包的路径
	private String dirname;
	
	public writetoSDcard(String filename, Context context){
		this.filename = filename;
		this.context = context; 
		
		hasSD = Environment.getExternalStorageState().equals( 
		android.os.Environment.MEDIA_MOUNTED); 
		SDpath = Environment.getExternalStorageDirectory().getPath(); 
		FILESPATH = this.context.getFilesDir().getPath(); 
	}
	
	public writetoSDcard(String dirname,String filename, Context context){
		this.filename = filename;
		this.context = context; 
		this.dirname = dirname;
		
		hasSD = Environment.getExternalStorageState().equals( 
		android.os.Environment.MEDIA_MOUNTED); 
		SDpath = Environment.getExternalStorageDirectory().getPath(); 
		FILESPATH = this.context.getFilesDir().getPath(); 
	}
	
	//在SD卡根目录下创建个文件
	public void directcreateinSDcard(){
		if(hasSD){
			File file = new File(SDpath + "//" + filename);
			if (!file.exists()) { 
				try {
					file.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				} 
			} 
		}else{
			return;
		}
		
	}
	
	//创建个文件夹在SD卡上
	public void createdirsSDcard(){
		if(hasSD){
			File dir = new File(SDpath + dirname);
			dir.mkdirs();
		}else{
			return;
		}
	}
	
	//在SD卡上创建一个文件袋文件夹
	public void createinSDcardwithdirs(){
		if(hasSD){
			File dir = new File(SDpath + dirname);
			dir.mkdirs();
			File file = new File(SDpath + dirname +"//" + filename);
			if (!file.exists()) { 
				try {
					file.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				} 
			} 
		}else{
			return;
		}
		
	}
}


//在手机内置存储中创建文件及文件夹
class writefileinlocal{
	private Context context;
	private String filename;
	private FileOutputStream fos;
	
	public writefileinlocal(String filename,Context context){
		this.context = context;
		this.filename = filename;
	}


	public FileOutputStream writelocalfile() throws FileNotFoundException{
		//打开手机内置存储的特定的文件夹,Context.MODE_PRIVATE为其他应用程序访问权限 ,
		fos=context.openFileOutput(filename, Context.MODE_PRIVATE);
		//得到output并返回
		return fos;
	}
	
	public String readfileinlocal() throws IOException{
		//打开手机内置存储卡的特定位置的文件,并打开其inputstream
		FileInputStream fin=context.openFileInput(filename);
		byte[] b=new byte[fin.available()];
		ByteArrayOutputStream buffer=new ByteArrayOutputStream();
		
		//讲其文件内容读到buffer中;
		while((fin.read(b))!=-1){
			buffer.write(b);
		}
		//将内容放在 字节数组data中
		byte[] data;
		data=buffer.toByteArray();
	
		buffer.close();
		fin.close();
		
		return new String(data);
	}
	
}

写了3小时的代码,可能其间还有些bug没测试。。。 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值