Ali*_*lin 117 resources android text
事情很简单,但不能按原样运作.
我有一个文本文件作为原始资源添加.文本文件包含如下文本:
b)如果适用法律要求对软件提供任何担保,则所有此类担保的有效期限为交付日期后的第九十(90)天.
(c)虚拟定向,其经销商,经销商,代理商或雇员提供的任何口头或书面信息或建议均不构成保证或以任何方式增加此处提供的任何保证的范围.
(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>
Run Code Online (Sandbox Code Playgroud)
读取原始资源的代码是:
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();
}
Run Code Online (Sandbox Code Playgroud)
文本得到了显示,但在每一行后我得到一个奇怪的字符[]如何删除该字符?我认为这是新线.
工作解决方案
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();
}
Run Code Online (Sandbox Code Playgroud)
小智 159
你可以用这个:
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.");
}
Run Code Online (Sandbox Code Playgroud)
wee*_*ens 63
如果使用基于字符的BufferedReader而不是基于字节的InputStream,该怎么办?
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = reader.readLine();
while (line != null) { ... }
Run Code Online (Sandbox Code Playgroud)
不要忘记readLine()
跳过新线!
tbr*_*aun 28
如果您使用来自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
Run Code Online (Sandbox Code Playgroud)
依赖关系:http://mvnrepository.com/artifact/commons-io/commons-io
Maven的:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
摇篮:
'commons-io:commons-io:2.4'
Run Code Online (Sandbox Code Playgroud)
Ars*_*ius 27
使用 Kotlin,您只需一行代码即可完成:
resources.openRawResource(R.raw.rawtextsample).bufferedReader().use { it.readText() }
Run Code Online (Sandbox Code Playgroud)
甚至声明扩展函数:
fun Resources.getRawTextFile(@RawRes id: Int) =
openRawResource(id).bufferedReader().use { it.readText() }
Run Code Online (Sandbox Code Playgroud)
然后直接使用它:
val txtFile = resources.getRawTextFile(R.raw.rawtextsample)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
174688 次 |
最近记录: |