Android阅读文本原始资源文件
事情很简单,但不应该如预期的那样工作。
我有一个文本文件添加为原始资源。 文本文件包含如下文本:
b)如果适用的法律要求对本软件作出任何保证,所有这些保证的有效期限均自交付之日起九十(90)天。
(c)虚拟定向,其经销商,分销商,代理商或员工提供的口头或书面信息或build议不应构成担保或以任何方式增加此处提供的任何担保的范围。
(d)(仅限美国)有些州不允许排除默示担保,因此上述排除可能不适用于您。 本保证赋予您特定的法律权利,您也可能享有其他法律权利,因国家不同而不同。
在我的屏幕上,我有这样的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1.0" android:layout_below="@+id/logoLayout" android:background="@drawable/list_background"> <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/txtRawResource" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="3dip"/> </ScrollView> </LinearLayout>
读取原始资源的代码是:
TextView txtRawResource= (TextView)findViewById(R.id.txtRawResource); txtDisclaimer.setText(Utils.readRawTextFile(ctx, R.raw.rawtextsample); public static String readRawTextFile(Context ctx, int resId) { InputStream inputStream = ctx.getResources().openRawResource(resId); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); int i; try { i = inputStream.read(); while (i != -1) { byteArrayOutputStream.write(i); i = inputStream.read(); } inputStream.close(); } catch (IOException e) { return null; } return byteArrayOutputStream.toString(); }
文本得到的显示,但每行后我得到一个奇怪的字符[]如何删除该字符? 我认为这是新的。
工作解决scheme
public static String readRawTextFile(Context ctx, int resId) { InputStream inputStream = ctx.getResources().openRawResource(resId); InputStreamReader inputreader = new InputStreamReader(inputStream); BufferedReader buffreader = new BufferedReader(inputreader); String line; StringBuilder text = new StringBuilder(); try { while (( line = buffreader.readLine()) != null) { text.append(line); text.append('\n'); } } catch (IOException e) { return null; } return text.toString(); }
如果使用基于字符的BufferedReader而不是基于字节的InputStream呢?
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line = reader.readLine(); while (line != null) { ... }
不要忘记,readLine()跳过了新行!
你可以使用这个:
try { Resources res = getResources(); InputStream in_s = res.openRawResource(R.raw.help); byte[] b = new byte[in_s.available()]; in_s.read(b); txtHelp.setText(new String(b)); } catch (Exception e) { // e.printStackTrace(); txtHelp.setText("Error: can't show help."); }
如果你使用apache的“commons-io”的IOUtils,那就更简单了:
InputStream is = getResources().openRawResource(R.raw.yourNewTextFile); String s = IOUtils.toString(is); IOUtils.closeQuietly(is); // don't forget to close your streams
依赖关系: http : //mvnrepository.com/artifact/commons-io/commons-io
Maven的:
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency>
摇篮:
'commons-io:commons-io:2.4'
而是这样做:
// reads resources regardless of their size public byte[] getResource(int id, Context context) throws IOException { Resources resources = context.getResources(); InputStream is = resources.openRawResource(id); ByteArrayOutputStream bout = new ByteArrayOutputStream(); byte[] readBuffer = new byte[4 * 1024]; try { int read; do { read = is.read(readBuffer, 0, readBuffer.length); if(read == -1) { break; } bout.write(readBuffer, 0, read); } while(true); return bout.toByteArray(); } finally { is.close(); } } // reads a string resource public String getStringResource(int id, Charset encoding) throws IOException { return new String(getResource(id, getContext()), encoding); } // reads an UTF-8 string resource public String getStringResource(int id) throws IOException { return new String(getResource(id, getContext()), Charset.forName("UTF-8")); }
从活动中添加
public byte[] getResource(int id) throws IOException { return getResource(id, this); }
或从一个testing用例中添加
public byte[] getResource(int id) throws IOException { return getResource(id, getContext()); }
并注意你的error handling – 当你的资源必须存在或者错误的时候,不要忽略和忽略exception。
这是另一种方法,肯定会工作,但我不能得到它读取多个文本文件在一个单一的活动在多个文字查看,任何人都可以帮忙吗?
TextView helloTxt = (TextView)findViewById(R.id.yourTextView); helloTxt.setText(readTxt()); } private String readTxt(){ InputStream inputStream = getResources().openRawResource(R.raw.yourTextFile); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); int i; try { i = inputStream.read(); while (i != -1) { byteArrayOutputStream.write(i); i = inputStream.read(); } inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return byteArrayOutputStream.toString(); }
@borislemke你可以通过类似的方式做到这一点
TextView tv ; findViewById(R.id.idOfTextView); tv.setText(readNewTxt()); private String readNewTxt(){ InputStream inputStream = getResources().openRawResource(R.raw.yourNewTextFile); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); int i; try { i = inputStream.read(); while (i != -1) { byteArrayOutputStream.write(i); i = inputStream.read(); } inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return byteArrayOutputStream.toString(); }
1.首先创build一个目录文件夹,并将其命名为res文件夹内2.在您之前创build的原始目录文件夹内创build一个.txt文件,并为其指定任何名称eg.articles.txt …. 3.复制并粘贴你想要在你创build的“.txt”.txt文件内的文本4.dont忘记在你的main.xml中包含一个textview MainActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gettingtoknowthe_os); TextView helloTxt = (TextView)findViewById(R.id.gettingtoknowos); helloTxt.setText(readTxt()); ActionBar actionBar = getSupportActionBar(); actionBar.hide();//to exclude the ActionBar } private String readTxt() { //getting the .txt file InputStream inputStream = getResources().openRawResource(R.raw.articles); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try { int i = inputStream.read(); while (i != -1) { byteArrayOutputStream.write(i); i = inputStream.read(); } inputStream.close(); } catch (IOException e) { e.printStackTrace(); } return byteArrayOutputStream.toString(); }
希望它的工作!
InputStream is=getResources().openRawResource(R.raw.name); BufferedReader reader=new BufferedReader(new InputStreamReader(is)); StringBuffer data=new StringBuffer(); String line=reader.readLine(); while(line!=null) { data.append(line+"\n"); } tvDetails.seTtext(data.toString());
这里是周末和Vovodroid解决scheme的组合。
它比Vovodroid的解决scheme更正确,比周一的解决scheme更完整。
try { InputStream inputStream = res.openRawResource(resId); try { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); try { StringBuilder result = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { result.append(line); } return result.toString(); } finally { reader.close(); } } finally { inputStream.close(); } } catch (IOException e) { // process exception }