潇潇洒洒

 

08_读取当前目录下得文本文件

在Asset目录下新建 texts/aa.txt  文件


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.AssetManager;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
  @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         TextView textView = new TextView(this);
         setContentView(textView);

        
         AssetManager assetManager = getAssets();
         InputStream inputStream = null;
         try {
             inputStream = assetManager.open("texts/aa.txt");
             String text = loadTextFile(inputStream);
             textView.setText(text);
         } catch (IOException e) {
             textView.setText("Couldn't load file");
         } finally {
             if (inputStream != null)
                 try {
                     inputStream.close();
                 } catch (IOException e) {
                     textView.setText("Couldn't close file");
                 }
         }
     }

     public String loadTextFile(InputStream inputStream) throws IOException {
         ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
         byte[] bytes = new byte[4096];
         int len = 0;
         while ((len = inputStream.read(bytes)) > 0)
             byteStream.write(bytes, 0, len);
         return new String(byteStream.toByteArray(), "UTF8");
     }
 }

posted on 2013-05-05 00:02  肖磊  阅读(135)  评论(0编辑  收藏  举报

导航