首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android读取文本原始资源文件

Android读取文本原始资源文件
EN

Stack Overflow用户
提问于 2010-11-03 21:49:40
回答 9查看 201.2K关注 0票数 132

我添加了一个文本文件作为原始资源。文本文件包含如下文本:

代码语言:javascript
复制
b) IF APPLICABLE LAW REQUIRES ANY WARRANTIES WITH RESPECT TO THE
SOFTWARE, ALL SUCH WARRANTIES ARE 
LIMITED IN DURATION TO NINETY (90)
DAYS FROM THE DATE OF  DELIVERY.

(c) NO ORAL OR WRITTEN INFORMATION OR
ADVICE GIVEN BY  VIRTUAL ORIENTEERING,
ITS DEALERS, DISTRIBUTORS, AGENTS OR
EMPLOYEES SHALL CREATE A WARRANTY OR
IN ANY WAY INCREASE THE SCOPE OF ANY
WARRANTY PROVIDED HEREIN. 

(d) (USA only) SOME STATES DO NOT
ALLOW THE EXCLUSION OF IMPLIED
WARRANTIES, SO THE ABOVE EXCLUSION MAY
NOT  APPLY TO YOU. THIS WARRANTY GIVES
YOU SPECIFIC LEGAL  RIGHTS AND YOU MAY
ALSO HAVE OTHER LEGAL RIGHTS THAT 
VARY FROM STATE TO STATE.

在我的屏幕上,我有一个这样的布局:

代码语言:javascript
复制
<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>

读取原始资源的代码为:

代码语言:javascript
复制
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();
}

显示了文本,但是在每一行之后,我得到了奇怪的字符[]。如何删除字符?我想这是一句新的话。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4087674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档