I want to read the text from a text file. In the code below, an exception occurs (that means it goes to the catch
block). I put the text file in the application folder. Where should I put this text file (mani.txt) in order to read it correctly?
我想从文本文件中读取文本。在下面的代码中,出现了一个异常(这意味着它会进入catch块)。我将文本文件放在应用程序文件夹中。我应该把这个文本文件(mani.txt)放在哪里,以便正确地读取它?
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();
}
7 个解决方案
#1
181
Try this :
试试这个:
I assume your text file is on sd card
我假设你的文本文件在sd卡上。
//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());
following links can also help you :
以下链接也可以帮助你:
How can I read a text file from the SD card in Android?
我如何在Android中读取SD卡上的文本文件?
How to read text file in Android?
如何在Android中读取文本文件?
Android read text raw resource file
Android读取文本原始资源文件。
#2
23
If you want to read file from sd card. Then following code might be helpful for you.
如果你想从sd卡读取文件。下面的代码可能对您有帮助。
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.
}
}
If you wan to read file from asset folder then
如果你想从资产文件夹中读取文件。
AssetManager am = context.getAssets();
InputStream is = am.open("test.txt");
Or If you wan to read this file from res/raw
foldery, where the file will be indexed and is accessible by an id in the R file:
或者,如果您想从res/raw foldery读取这个文件,该文件将被索引,并且可以通过R文件中的id访问:
InputStream is = getResources().openRawResource(R.raw.test);
Good example of reading text file from res/raw folder
从res/raw文件夹中读取文本文件的好例子。
#3
6
Put your text file in Asset Folder...& read file form that folder...
将你的文本文件放入资产文件夹…和读取文件格式的文件夹…
see below reference links...
参见下面参考链接……
http://www.technotalkative.com/android-read-file-from-assets/
http://www.technotalkative.com/android-read-file-from-assets/
http://sree.cc/google/reading-text-file-from-assets-folder-in-android
http://sree.cc/google/reading-text-file-from-assets-folder-in-android
阅读一个简单的文本文件。
hope it will help...
希望这将有助于……
#4
2
First you store your text file in to raw folder.
首先将文本文件存储到原始文件夹中。
private void loadWords() throws IOException {
Log.d(TAG, "Loading words...");
final Resources resources = mHelperContext.getResources();
InputStream inputStream = resources.openRawResource(R.raw.definitions);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String line;
while ((line = reader.readLine()) != null) {
String[] strings = TextUtils.split(line, "-");
if (strings.length < 2)
continue;
long id = addWord(strings[0].trim(), strings[1].trim());
if (id < 0) {
Log.e(TAG, "unable to add word: " + strings[0].trim());
}
}
} finally {
reader.close();
}
Log.d(TAG, "DONE loading words.");
}
#5
0
Try this
试试这个
try {
reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String line="";
String s ="";
try
{
line = reader.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
while (line != null)
{
s = s + line;
s =s+"\n";
try
{
line = reader.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
}
tv.setText(""+s);
}
#6
0
Try this code
试试这个代码
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;
}
#7
-4
// working
public static String getStringFromFile (String filename) throws Exception {
//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,filename);
FileInputStream fin = new FileInputStream(file);
String ret = convertStreamToString(fin);
//Make sure you close all streams.
fin.close();
return ret;
}
#1
181
Try this :
试试这个:
I assume your text file is on sd card
我假设你的文本文件在sd卡上。
//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());
following links can also help you :
以下链接也可以帮助你:
How can I read a text file from the SD card in Android?
我如何在Android中读取SD卡上的文本文件?
How to read text file in Android?
如何在Android中读取文本文件?
Android read text raw resource file
Android读取文本原始资源文件。
#2
23
If you want to read file from sd card. Then following code might be helpful for you.
如果你想从sd卡读取文件。下面的代码可能对您有帮助。
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.
}
}
If you wan to read file from asset folder then
如果你想从资产文件夹中读取文件。
AssetManager am = context.getAssets();
InputStream is = am.open("test.txt");
Or If you wan to read this file from res/raw
foldery, where the file will be indexed and is accessible by an id in the R file:
或者,如果您想从res/raw foldery读取这个文件,该文件将被索引,并且可以通过R文件中的id访问:
InputStream is = getResources().openRawResource(R.raw.test);
Good example of reading text file from res/raw folder
从res/raw文件夹中读取文本文件的好例子。
#3
6
Put your text file in Asset Folder...& read file form that folder...
将你的文本文件放入资产文件夹…和读取文件格式的文件夹…
see below reference links...
参见下面参考链接……
http://www.technotalkative.com/android-read-file-from-assets/
http://www.technotalkative.com/android-read-file-from-assets/
http://sree.cc/google/reading-text-file-from-assets-folder-in-android
http://sree.cc/google/reading-text-file-from-assets-folder-in-android
阅读一个简单的文本文件。
hope it will help...
希望这将有助于……
#4
2
First you store your text file in to raw folder.
首先将文本文件存储到原始文件夹中。
private void loadWords() throws IOException {
Log.d(TAG, "Loading words...");
final Resources resources = mHelperContext.getResources();
InputStream inputStream = resources.openRawResource(R.raw.definitions);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String line;
while ((line = reader.readLine()) != null) {
String[] strings = TextUtils.split(line, "-");
if (strings.length < 2)
continue;
long id = addWord(strings[0].trim(), strings[1].trim());
if (id < 0) {
Log.e(TAG, "unable to add word: " + strings[0].trim());
}
}
} finally {
reader.close();
}
Log.d(TAG, "DONE loading words.");
}
#5
0
Try this
试试这个
try {
reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String line="";
String s ="";
try
{
line = reader.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
while (line != null)
{
s = s + line;
s =s+"\n";
try
{
line = reader.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
}
tv.setText(""+s);
}
#6
0
Try this code
试试这个代码
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;
}
#7
-4
// working
public static String getStringFromFile (String filename) throws Exception {
//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,filename);
FileInputStream fin = new FileInputStream(file);
String ret = convertStreamToString(fin);
//Make sure you close all streams.
fin.close();
return ret;
}