Android读取文本原始资源文件

138

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

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.

我屏幕上的布局如下:

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

文本已显示,但每行后面都有奇怪的字符[]。我该如何去除这些字符?我认为这是一个换行符。

3
请将原始参数注释为@RawRes,这样Android Studio才能识别原始资源。 - Roel
1
工作解决方案应该发布为答案,这样它可以被投票支持。 - LarsH
14个回答

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

1

1.首先在res文件夹里创建一个名为raw的目录文件夹 2.在之前创建的raw目录文件夹中创建一个.txt文件,并给它任何名称,比如articles.txt... 3.将您想要的文本复制并粘贴到您创建的.txt文件"articles.txt"中 4.不要忘记在您的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();
}

希望它能够正常工作!

0
val inputStream: InputStream = resources.openRawResource(R.raw.product_json)
val reader: Reader = BufferedReader(InputStreamReader(inputStream, "utf-8"))

val writer: Writer = StringWriter()
val buffer = CharArray(1024)
reader.use { it ->
    var n: Int
    while (it.read(buffer).also { n = it } != -1) {
        writer.write(buffer, 0, n)
    }
}
val stringVal = writer.toString()

0

这里有一个一行代码:

String text = new BufferedReader(
  new InputStreamReader(getResources().openRawResource(R.raw.my_file)))
  .lines().reduce("\n", (a,b) -> a+b);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接