Android 读取Assets下的文件
Android读取Assets下的文件,在Assets下面有个text.txt文件
MainActivity的布局文件中放置一个 TextView文件,
下面看java代码
public String getFromAssets(String fileName){ String Result=""; try { InputStreamReader inputReader = new InputStreamReader( getResources().getAssets().open(fileName) ); BufferedReader bufReader = new BufferedReader(inputReader); String line=""; while((line = bufReader.readLine()) != null) Result += line; } catch (Exception e) { e.printStackTrace(); } return Result; }
上面是读取Assets下文件
private TextView view; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); view=(TextView)findViewById(R.id.view); String result=getFromAssets("text.txt"); view.setText(Html.fromHtml(result)); }
下面看运行效果