android读取文本原始资源文件

kkbh8khc  于 2021-07-03  发布在  Java
关注(0)|答案(12)|浏览(500)

事情很简单,但没有按预期的那样进行。
我添加了一个文本文件作为原始资源。文本文件包含如下文本:
b) 如果适用法律要求对本软件作出任何保证,则所有此类保证的有效期均限于自交付之日起九十(90)天。
(c) 虚拟定向运动、其经销商、分销商、代理商或员工提供的任何口头或书面信息或建议均不得构成担保或以任何方式扩大本协议规定的任何担保范围。
(d) (仅限美国)有些州不允许排除默示担保,因此上述排除可能不适用于您。本保证为您提供了特定的法律权利,您还可能拥有因州而异的其他法律权利。
在我的屏幕上,我有这样一个布局:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="wrap_content"
  4. android:gravity="center"
  5. android:layout_weight="1.0"
  6. android:layout_below="@+id/logoLayout"
  7. android:background="@drawable/list_background">
  8. <ScrollView android:layout_width="fill_parent"
  9. android:layout_height="fill_parent">
  10. <TextView android:id="@+id/txtRawResource"
  11. android:layout_width="fill_parent"
  12. android:layout_height="fill_parent"
  13. android:padding="3dip"/>
  14. </ScrollView>
  15. </LinearLayout>

读取原始资源的代码是:

  1. TextView txtRawResource= (TextView)findViewById(R.id.txtRawResource);
  2. txtDisclaimer.setText(Utils.readRawTextFile(ctx, R.raw.rawtextsample);
  3. public static String readRawTextFile(Context ctx, int resId)
  4. {
  5. InputStream inputStream = ctx.getResources().openRawResource(resId);
  6. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  7. int i;
  8. try {
  9. i = inputStream.read();
  10. while (i != -1)
  11. {
  12. byteArrayOutputStream.write(i);
  13. i = inputStream.read();
  14. }
  15. inputStream.close();
  16. } catch (IOException e) {
  17. return null;
  18. }
  19. return byteArrayOutputStream.toString();
  20. }

文本get已经显示,但是每行之后我都会得到一个奇怪的字符[]如何删除该字符?我想这是新的路线。
工作溶液

  1. public static String readRawTextFile(Context ctx, int resId)
  2. {
  3. InputStream inputStream = ctx.getResources().openRawResource(resId);
  4. InputStreamReader inputreader = new InputStreamReader(inputStream);
  5. BufferedReader buffreader = new BufferedReader(inputreader);
  6. String line;
  7. StringBuilder text = new StringBuilder();
  8. try {
  9. while (( line = buffreader.readLine()) != null) {
  10. text.append(line);
  11. text.append('\n');
  12. }
  13. } catch (IOException e) {
  14. return null;
  15. }
  16. return text.toString();
  17. }
hgqdbh6s

hgqdbh6s1#

这是另一种方法,肯定会工作,但我不能让它读取多个文本文件,以查看在多个文本视图在一个单一的活动,任何人都可以帮助?

  1. TextView helloTxt = (TextView)findViewById(R.id.yourTextView);
  2. helloTxt.setText(readTxt());
  3. }
  4. private String readTxt(){
  5. InputStream inputStream = getResources().openRawResource(R.raw.yourTextFile);
  6. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  7. int i;
  8. try {
  9. i = inputStream.read();
  10. while (i != -1)
  11. {
  12. byteArrayOutputStream.write(i);
  13. i = inputStream.read();
  14. }
  15. inputStream.close();
  16. } catch (IOException e) {
  17. // TODO Auto-generated catch block
  18. e.printStackTrace();
  19. }
  20. return byteArrayOutputStream.toString();
  21. }
展开查看全部
gdx19jrr

gdx19jrr2#

以下是从原始文件夹读取文本文件的简单方法:

  1. public static String readTextFile(Context context,@RawRes int id){
  2. InputStream inputStream = context.getResources().openRawResource(id);
  3. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  4. byte buffer[] = new byte[1024];
  5. int size;
  6. try {
  7. while ((size = inputStream.read(buffer)) != -1) {
  8. outputStream.write(buffer, 0, size);
  9. }
  10. outputStream.close();
  11. inputStream.close();
  12. } catch (IOException e) {
  13. }
  14. return outputStream.toString();
  15. }
展开查看全部
vd8tlhqk

vd8tlhqk3#

下面是weekens和vovodroid的混合解决方案。
它比vovodroid解更正确,比weekens解更完整。

  1. try {
  2. InputStream inputStream = res.openRawResource(resId);
  3. try {
  4. BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
  5. try {
  6. StringBuilder result = new StringBuilder();
  7. String line;
  8. while ((line = reader.readLine()) != null) {
  9. result.append(line);
  10. }
  11. return result.toString();
  12. } finally {
  13. reader.close();
  14. }
  15. } finally {
  16. inputStream.close();
  17. }
  18. } catch (IOException e) {
  19. // process exception
  20. }
展开查看全部
oxalkeyp

oxalkeyp4#

如果使用apache“commons io”中的ioutils,则更容易:

  1. InputStream is = getResources().openRawResource(R.raw.yourNewTextFile);
  2. String s = IOUtils.toString(is);
  3. IOUtils.closeQuietly(is); // don't forget to close your streams

依赖项:http://mvnrepository.com/artifact/commons-io/commons-io
Maven:

  1. <dependency>
  2. <groupId>commons-io</groupId>
  3. <artifactId>commons-io</artifactId>
  4. <version>2.4</version>
  5. </dependency>

grad尔:

  1. 'commons-io:commons-io:2.4'
展开查看全部
q8l4jmvw

q8l4jmvw5#

有了kotlin,你只需要一行代码:

  1. resources.openRawResource(R.raw.rawtextsample).bufferedReader().use { it.readText() }

甚至声明扩展函数:

  1. fun Resources.getRawTextFile(@RawRes id: Int) =
  2. openRawResource(id).bufferedReader().use { it.readText() }

然后直接使用它:

  1. val txtFile = resources.getRawTextFile(R.raw.rawtextsample)
bvk5enib

bvk5enib6#

@borislemke你可以用类似的方法

  1. TextView tv ;
  2. findViewById(R.id.idOfTextView);
  3. tv.setText(readNewTxt());
  4. private String readNewTxt(){
  5. InputStream inputStream = getResources().openRawResource(R.raw.yourNewTextFile);
  6. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  7. int i;
  8. try {
  9. i = inputStream.read();
  10. while (i != -1)
  11. {
  12. byteArrayOutputStream.write(i);
  13. i = inputStream.read();
  14. }
  15. inputStream.close();
  16. } catch (IOException e) {
  17. // TODO Auto-generated catch block
  18. e.printStackTrace();
  19. }
  20. return byteArrayOutputStream.toString();
  21. }
展开查看全部
tp5buhyn

tp5buhyn7#

您可以使用:

  1. try {
  2. Resources res = getResources();
  3. InputStream in_s = res.openRawResource(R.raw.help);
  4. byte[] b = new byte[in_s.available()];
  5. in_s.read(b);
  6. txtHelp.setText(new String(b));
  7. } catch (Exception e) {
  8. // e.printStackTrace();
  9. txtHelp.setText("Error: can't show help.");
  10. }
yftpprvb

yftpprvb8#

下面是kotlin中的一个实现

  1. try {
  2. val inputStream: InputStream = this.getResources().openRawResource(R.raw.**)
  3. val inputStreamReader = InputStreamReader(inputStream)
  4. val sb = StringBuilder()
  5. var line: String?
  6. val br = BufferedReader(inputStreamReader)
  7. line = br.readLine()
  8. while (line != null) {
  9. sb.append(line)
  10. line = br.readLine()
  11. }
  12. br.close()
  13. var content : String = sb.toString()
  14. Log.d(TAG, content)
  15. } catch (e:Exception){
  16. Log.d(TAG, e.toString())
  17. }
展开查看全部
yqkkidmi

yqkkidmi9#

不如这样做:

  1. // reads resources regardless of their size
  2. public byte[] getResource(int id, Context context) throws IOException {
  3. Resources resources = context.getResources();
  4. InputStream is = resources.openRawResource(id);
  5. ByteArrayOutputStream bout = new ByteArrayOutputStream();
  6. byte[] readBuffer = new byte[4 * 1024];
  7. try {
  8. int read;
  9. do {
  10. read = is.read(readBuffer, 0, readBuffer.length);
  11. if(read == -1) {
  12. break;
  13. }
  14. bout.write(readBuffer, 0, read);
  15. } while(true);
  16. return bout.toByteArray();
  17. } finally {
  18. is.close();
  19. }
  20. }
  21. // reads a string resource
  22. public String getStringResource(int id, Charset encoding) throws IOException {
  23. return new String(getResource(id, getContext()), encoding);
  24. }
  25. // reads an UTF-8 string resource
  26. public String getStringResource(int id) throws IOException {
  27. return new String(getResource(id, getContext()), Charset.forName("UTF-8"));
  28. }

从活动中,添加

  1. public byte[] getResource(int id) throws IOException {
  2. return getResource(id, this);
  3. }

或者从测试用例中,添加

  1. public byte[] getResource(int id) throws IOException {
  2. return getResource(id, getContext());
  3. }

注意你的错误处理——当你的资源必须存在或者(非常?)出错时,不要捕捉并忽略异常。

展开查看全部
omjgkv6w

omjgkv6w10#

  1. InputStream is=getResources().openRawResource(R.raw.name);
  2. BufferedReader reader=new BufferedReader(new InputStreamReader(is));
  3. StringBuffer data=new StringBuffer();
  4. String line=reader.readLine();
  5. while(line!=null)
  6. {
  7. data.append(line+"\n");
  8. }
  9. tvDetails.seTtext(data.toString());
wgxvkvu9

wgxvkvu911#

如果使用基于字符的bufferedreader而不是基于字节的inputstream呢?

  1. BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
  2. String line = reader.readLine();
  3. while (line != null) {
  4. ...
  5. line = reader.readLine();
  6. }

别忘了 readLine() 跳过新台词!

83qze16e

83qze16e12#

1.首先在res文件夹中创建一个目录文件夹,并将其命名为raw;2.在先前创建的raw目录文件夹中创建一个.txt文件,并将其命名为任何名称,如articles.txt。。。。3.将所需文本复制并粘贴到您创建的.txt文件“articles.txt”中4.不要忘记在main.xml mainactivity.java中包含文本视图

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_gettingtoknowthe_os);
  5. TextView helloTxt = (TextView)findViewById(R.id.gettingtoknowos);
  6. helloTxt.setText(readTxt());
  7. ActionBar actionBar = getSupportActionBar();
  8. actionBar.hide();//to exclude the ActionBar
  9. }
  10. private String readTxt() {
  11. //getting the .txt file
  12. InputStream inputStream = getResources().openRawResource(R.raw.articles);
  13. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  14. try {
  15. int i = inputStream.read();
  16. while (i != -1) {
  17. byteArrayOutputStream.write(i);
  18. i = inputStream.read();
  19. }
  20. inputStream.close();
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. return byteArrayOutputStream.toString();
  25. }

希望成功!

展开查看全部

相关问题