Android 读取资源文件

来源:互联网 发布:手机遗像制作软件 编辑:程序博客网 时间:2024/05/16 13:58

1.首先在res的文件夹下新建一个raw的文件夹,把需要读取的文件放到raw文件夹中,

package com.example.fileoperate;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Scanner;

import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore.Files;
import android.app.Activity;
import android.content.res.Resources;
import android.drm.DrmInfoRequest;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

 private TextView msg;

 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  msg = (TextView) 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");
   
  }
  try {
   scan.close();
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  }
  msg.setText(buf);
  
 }

 

0 0