我如何读取.txt和Android中显示为TextView的? [英] How can I read .txt and display it as TextView in Android?

查看:198
本文介绍了我如何读取.txt和Android中显示为TextView的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是code以下,开始读SD卡中的txt文件,但如何将其显示为文本视图?

  {尝试               文件f =新的文件(Environment.getExternalStorageDirectory()+/ FILENAME.TXT);
               的FileInputStream fileIS =新的FileInputStream(F);
               BUF的BufferedReader =新的BufferedReader(新的InputStreamReader(fileIS));
               字符串readString =新的String();
               //只是阅读每一行,并把它传递调试程序
               而((readString = buf.readLine())!= NULL){
                  Log.d(行,readString);               }            }赶上(FileNotFoundException异常五){               e.printStackTrace();            }赶上(IOException异常五){               e.printStackTrace();            }


解决方案

这应该这样做:

 公共无效displayOutput()
{
    文件SD卡= Environment.getExternalStorageDirectory();
    档案文件=新的文件(SD卡,/ TextFile.txt的);
    StringBuilder的文本=新的StringBuilder();
    尝试{
        BR的BufferedReader =新的BufferedReader(新的FileReader(文件));
        串线;
        而((行= br.readLine())!= NULL){
            text.append(线);
            text.append('\\ n');
        }
    }
    赶上(IOException异常五){
        Toast.makeText(getApplicationContext(),读取文件错误!,Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
    赶上(FileNotFoundException异常五){
        Toast.makeText(getApplicationContext(),找不到文件!,Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
    TextView的输出=(的TextView)findViewById(R.id.output);
    //假设输出是你的TextView的id
    output.setText(文本);
}

I am using the code below to read the the txt files from my SD card, but how can I display it as Text View?

try{

               File f = new File(Environment.getExternalStorageDirectory()+"/filename.txt");
               FileInputStream fileIS = new FileInputStream(f);
               BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
               String readString = new String();
               //just reading each line and pass it on the debugger  
               while((readString = buf.readLine())!= null){
                  Log.d("line: ", readString);

               }

            } catch (FileNotFoundException e) {

               e.printStackTrace();

            } catch (IOException e){

               e.printStackTrace();

            }

解决方案

This should do it :

public void displayOutput()
{
    File sdcard = Environment.getExternalStorageDirectory();
    File file = new File(sdcard,"/TextFile.txt");
    StringBuilder text = new StringBuilder();
    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while ((line = br.readLine()) != null) {
            text.append(line);
            text.append('\n');
        }
    }
    catch (IOException e) {
        Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show();
        e.printStackTrace();
    } 
    catch (FileNotFoundException e) {
        Toast.makeText(getApplicationContext(),"File not found!",Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
    TextView output=(TextView) findViewById(R.id.output); 
    // Assuming that 'output' is the id of your TextView
    output.setText(text);
}

这篇关于我如何读取.txt和Android中显示为TextView的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆