zoukankan      html  css  js  c++  java
  • 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");
         }
     }

  • 相关阅读:
    一个简单实现的遮罩层
    文字内容过长是自动出现省略号
    Maven入门学习(下)
    Maven入门学习(上)
    使用ODP.NET查询数据参数顺序问题及莫名ORA-01722错误提示
    Xamarin开发Android笔记:TextView行间距设定
    Xamarin开发Android笔记:图片切换ImageSwitcher
    Xamarin开发Android笔记:背景操作
    UI创意求助:手机贪吃蛇游戏方向控制键设计
    做梦想起来的C#简单实现贪吃蛇程序(LinQ + Entity)
  • 原文地址:https://www.cnblogs.com/xl711436/p/3060438.html
Copyright © 2011-2022 走看看