【数据存储】操作资源文件

package org.lxh.demo;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TextView;

public class FileOperate extends Activity {
    private TextView msg = null ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.main);
        this.msg = (TextView) super.findViewById(R.id.msg) ;
        // 资源操作类
        Resources res = super.getResources() ;    
        // 为要读取的内容设置输入流
        InputStream input = res.openRawResource(R.raw.mybook) ;    
        Scanner scan = new Scanner(input) ;
        // 接收数据
        StringBuffer buf = new StringBuffer() ;
        // 循环读取
        while (scan.hasNext()) {  
            // 保存数据
            buf.append(scan.next()).append("\n") ;
        }
        scan.close() ;
        try {
            input.close() ;
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.msg.setText(buf) ;
    }
}

原文地址:https://www.cnblogs.com/androidsj/p/3127175.html