Android基础篇 读取 Assets 文件夹中的文件

一、前言

(1)资源类型

第一种:res目录下的资源(该资源不会被编译,但是会生成id)
第二种:Assets文件夹下的资源文件,又叫原始资源文件(不会被编译,也不会生成id)

(2)Assets概念以及创建

assets 文件夹用于存储应用需要的文件,在安装后可直接从其中读取使用或者写入本地存储中

Android Studio 默认不建立该文件夹,可以手动新建 : app -> src -> main -> assets

或者,右键 main -> New -> Folder -> Assets Folder

Android基础篇 读取 Assets 文件夹中的文件_第1张图片
AssetManager 对象可以直接访问该文件夹:

AssetManager assetManager = this.getApplicationContext().getAssets();

使用函数 open 可以打开 assets 文件夹中对象,返回一个 InputStream 对象:

//"label.txt" 需要携带尾缀 + 文件名称
inputStream = assetManager.open("label.txt");

二、【读取】assets中文本文件

 private String readStringFromAssets() {
 	//通过设备管理对象 获取Asset的资源路径
	 AssetManager assetManager = this.getApplicationContext().getAssets();
	
	  InputStream inputStream = null;
	  InputStreamReader isr = null;
	  BufferedReader br = null;
	
	  StringBuffer sb =  new StringBuffer();
	  try{
		inputStream = assetManager.open("label.txt");
		isr = new InputStreamReader(inputStream);
		br = new BufferedReader(isr);
		
		sb.append(br.readLine());
		String line = null;
		while((line = br.readLine()) != null){
			 sb.append("\n" + line);
			}
			
	        br.close();
            isr.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (isr != null) {
                    isr.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
}

三、读取assets中文件,【写入】本地

 /* Checks if external storage is available for read and write */
    public boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        Log.e(TAG, "isExternalStorageWritable: " + state);
        return false;
    }

private boolean readDataFromAssets() {
    if (!isExternalStorageWritable()) {
        return false;
    }
	
	InputStream inputStream = null;
	FileOutputStream fos = null;
	BufferedOutStream bos = null;
	
	File dir = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
    Log.e(TAG, "readDataFromAssets: dir = " + dir.getAbsolutePath());

	 if (!dir.exists()) {
        dir.mkdirs();
     }
	
	  try{
	  	
		inputStream = getAssets().open("app-debug.apk");
		
		File file = new File(dir, "app-debug.apk");
		
		if (file.exists()) {
            file.delete();
        }
        file.createNewFile();
		
		fos = new FileOutputStream(file);
        bos = new BufferedOutputStream(fos);
		
		byte[] bytes = new byte[1024];
		
		while(inputStream.read(bytes) > 0){
        bos.write(bytes,0,bytes.lenght);
        }
        
		inputStream.close();
		
        bos.close();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
            if (bos != null) {
                bos.close();
            }
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return true;
}

你可能感兴趣的:(Android基础篇,android)