首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Android中读取文本文件?

如何在Android中读取文本文件?
EN

Stack Overflow用户
提问于 2012-09-14 17:35:10
回答 6查看 353.8K关注 0票数 127

我想从文本文件中读取文本。在下面的代码中,发生了一个异常(这意味着它转到了catch块)。我将文本文件放在应用程序文件夹中。我应该将这个文本文件(mani.txt)放在哪里才能正确读取它?

代码语言:javascript
复制
    try
    {
        InputStream instream = openFileInput("E:\\test\\src\\com\\test\\mani.txt"); 
        if (instream != null)
        {
            InputStreamReader inputreader = new InputStreamReader(instream); 
            BufferedReader buffreader = new BufferedReader(inputreader); 
            String line,line1 = "";
            try
            {
                while ((line = buffreader.readLine()) != null)
                    line1+=line;
            }catch (Exception e) 
            {
                e.printStackTrace();
            }
         }
    }
    catch (Exception e) 
    {
        String error="";
        error=e.getMessage();
    }
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2012-09-14 17:38:32

试试这个:

我假设你的文本文件在SD卡上

代码语言:javascript
复制
    //Find the directory for the SD Card using the API
//*Don't* hardcode "/sdcard"
File sdcard = Environment.getExternalStorageDirectory();

//Get the text file
File file = new File(sdcard,"file.txt");

//Read text from file
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');
    }
    br.close();
}
catch (IOException e) {
    //You'll need to add proper error handling here
}

//Find the view by its id
TextView tv = (TextView)findViewById(R.id.text_view);

//Set the text
tv.setText(text.toString());

以下链接也可以帮助您:

How can I read a text file from the SD card in Android?

How to read text file in Android?

Android read text raw resource file

票数 260
EN

Stack Overflow用户

发布于 2012-09-14 17:48:53

如果你想从SD卡中读取文件。那么下面的代码可能会对你有所帮助。

代码语言:javascript
复制
 StringBuilder text = new StringBuilder();
    try {
    File sdcard = Environment.getExternalStorageDirectory();
    File file = new File(sdcard,"testFile.txt");

        BufferedReader br = new BufferedReader(new FileReader(file));  
        String line;   
        while ((line = br.readLine()) != null) {
                    text.append(line);
                    Log.i("Test", "text : "+text+" : end");
                    text.append('\n');
                    } }
    catch (IOException e) {
        e.printStackTrace();                    

    }
    finally{
            br.close();
    }       
    TextView tv = (TextView)findViewById(R.id.amount);  

    tv.setText(text.toString()); ////Set the text to text view.
  }

    }

如果您想从资产文件夹中读取文件,则

代码语言:javascript
复制
AssetManager am = context.getAssets();
InputStream is = am.open("test.txt");

或者,如果您想要从res/raw文件夹中读取此文件,该文件将被索引,并可通过R文件中的id进行访问:

代码语言:javascript
复制
InputStream is = getResources().openRawResource(R.raw.test);     

Good example of reading text file from res/raw folder

票数 31
EN

Stack Overflow用户

发布于 2015-12-31 16:39:58

试试这段代码

代码语言:javascript
复制
public static String pathRoot = "/sdcard/system/temp/";
public static String readFromFile(Context contect, String nameFile) {
    String aBuffer = "";
    try {
        File myFile = new File(pathRoot + nameFile);
        FileInputStream fIn = new FileInputStream(myFile);
        BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
        String aDataRow = "";
        while ((aDataRow = myReader.readLine()) != null) {
            aBuffer += aDataRow;
        }
        myReader.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return aBuffer;
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12421814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档