android读取工程里文件并显示在界面
16lz
2021-12-04
package com.example.and_2;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.widget.TextView;public class MainActivity extends Activity {private TextView tv1;private String result_weather="";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tv1=(TextView)findViewById(R.id.tv1);tv1.setText(read());}public String read() {StringBuilder sb = new StringBuilder();String data,str;BufferedReader br;try {InputStream input = getResources().openRawResource(R.raw.invalid_weather);//BufferedReader read = new BufferedReader(new InputStreamReader(input));br = new BufferedReader(new InputStreamReader(input,"GBK"));/////UTF-8也是亂碼while ((data = br.readLine()) != null) // 判断最后一行不存在,为空结束循环{// System.out.println(data);//原样输出读到的内容sb.append(data);sb.append("\r\n");}br.close();str=sb.toString();//System.out.println(str);Log.i("str", str);return str;} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return "";}}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /></RelativeLayout>
更多相关文章
- android:Handler开启线程定时循环
- android电话录音
- android水平循环滚动控件
- Android(安卓)Io 整理
- android中IO操作数据总结
- %1$s %1$d Android(安卓)string (java & Android(安卓)格式化字符
- android单元测试用例和日志输出
- Android(安卓)Studio中使用NDK
- Android运行底层linux外部命令的实现