事情很简单,但不应按预期工作。Android阅读文本原始资源文件
我有一个文本文件作为原始资源添加。该文本文件包含文本,如:
B)如果适用法律要求提供与对于 软件的任何担保,所有此类担保在 有效期仅限于九十(自交货之日起90) DAYS 。
(c)中的口头或书面的信息或 建议都VIRTUAL定向, ITS经销商,分销商,代理或 雇员将CREATE以任何方式保证或 提高任何 担保本文提供的范围。
(d)(美国只)一些州不 ALLOW暗示 担保的排除,因此上述排除可能 不适用于您。本保修提供 您具体的法律权利,您可能还有其他法律权利,因为这些 不同国家之间不同。
在我的屏幕我有一个这样的布局:
<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();
}
文本得到的显示,但在每行后,我收到了奇怪的人物[]如何删除该人物?我认为这是新线。
工作液
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();
}
我不知道该Inputstream.available()这里是正确的选择,而读取n到一个ByteArrayOutputStream直到n ==可-1。 – ThomasRS 2012-04-30 08:32:22
这可能无法适用于大型资源。它取决于输入流读取缓冲区的大小,并且只能返回一部分资源。 – d4n3 2012-06-29 06:19:13
@ d4n3是正确的,输入流可用方法的文档状态为:“返回可读取或跳过的字节的估计数量,不会阻塞更多的输入 请注意,此方法提供了这样一个弱保证,它不是在实践中非常有用“ – ozba 2013-04-10 14:18:30