安卓阅读文本的原始资源文件

安卓阅读文本的原始资源文件

本文介绍了安卓阅读文本的原始资源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一切都简单但应该不工作。

我添加为原料资源的文本文件。该文本文件包含类似文本:

    

(三)任何口头或书面的信息或  建议都虚拟定向越野,  ITS经销商,分销商,代理商或  员工应创建一个保证或  以任何方式增加的任何范围  保证此处提供。

    

(D)(仅限美国)有些国家不  不允许对暗示的排除  担保,因此以上排除可能  并不适用于您。本保证给予  您特定的法律权利,你可能  还具有其他法律权利,这  取决于各个州。

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

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
                     机器人:layout_width =FILL_PARENT
                     机器人:layout_height =WRAP_CONTENT
                     机器人:重力=中心
                     机器人:layout_weight =1.0
                     机器人:layout_below =@ + ID / logoLayout
                     机器人:背景=@可绘制/ list_background>

            <滚动型机器人:layout_width =FILL_PARENT
                        机器人:layout_height =FILL_PARENT>

                    < TextView的机器人:ID =@ + ID / txtRawResource
                               机器人:layout_width =FILL_PARENT
                               机器人:layout_height =FILL_PARENT
                               机器人:填充=3dip/>
            < /滚动型>

    < / LinearLayout中>
 

在code读取原始资源是:

  TextView的txtRawResource =(TextView中)findViewById(R.id.txtRawResource);

txtDisclaimer.setText(Utils.readRawTextFile(CTX,R.raw.rawtextsample);

公共静态字符串readRawTextFile(上下文CTX,INT渣油)
{
    InputStream中的InputStream = ctx.getResources()openRawResource(渣油)。

    ByteArrayOutputStream byteArrayOutputStream =新ByteArrayOutputStream();

    INT I;
    尝试 {
        I = inputStream.read();
        而(ⅰ!= -1)
        {
            byteArrayOutputStream.write(ⅰ);
            I = inputStream.read();
        }
        inputStream.close();
    }赶上(IOException异常E){
        返回null;
    }
    返回byteArrayOutputStream.toString();
}
 

正文得到的表现,但在每行后,我收到了奇怪的字符[]如何删除角色?我认为这是新线。

有效的解决方案。

 公共静态字符串readRawTextFile(上下文CTX,INT渣油)
{
    InputStream中的InputStream = ctx.getResources()openRawResource(渣油)。

    InputStreamReader的inputreader =新的InputStreamReader(InputStream的);
    的BufferedReader buffreader =新的BufferedReader(inputreader);
    串线;
    StringBuilder的文本=新的StringBuilder();

    尝试 {
        而((行= buffreader.readLine())!= NULL){
            text.append(线);
            text.append('\ N');
        }
    }赶上(IOException异常E){
        返回null;
    }
    返回text.toString();
}
 

解决方案

如果您使用基于字节的InputStream的基于字符的BufferedReader呢?

 的BufferedReader读卡器=新的BufferedReader(新的InputStreamReader(InputStream中));
串线= reader.readLine();
而(行!= NULL){...}
 

不要忘了的readLine()跳过新行!

Things are simple but don't work as supposed to.

I have a text file added as a raw resource. The text file contains text like:

(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.

On my screen I have a layout like this:

<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>

The code to read the raw resource is:

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();
}

The text get's showed but after each line I get a strange character [] How can I remove that character ? I think it's New Line.

WORKING SOLUTION

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();
}
解决方案

What if you use a character-based BufferedReader instead of byte-based InputStream?

BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = reader.readLine();
while (line != null) { ... }

Don't forget that readLine() skips the new-lines!

这篇关于安卓阅读文本的原始资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:35