Reading a simple text file

我试图在我的 Android 应用程序示例中读取一个简单的文本文件。我使用下面写的代码读取简单的文本文件。

InputStream inputStream = openFileInput("test.txt");
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

我的问题是: 我应该把这个 "test.txt"文件放在我的项目中的什么地方?.我已经尝试把文件下的 "res/raw""asset"文件夹,但我得到的 exception "FileNotFound"时,第一个生活的代码写上面得到执行。

212687 次浏览

Place your text file in the /assets directory under the Android project. Use AssetManager class to access it.

AssetManager am = context.getAssets();
InputStream is = am.open("test.txt");

或者你也可以把文件放在 /res/raw目录中,在那里文件将被索引,并且可以通过 R 文件中的 id 来访问:

InputStream is = context.getResources().openRawResource(R.raw.test);

Having a file in your assets folder requires you to use this piece of code in order to get files from the assets folder:

yourContext.getAssets().open("test.txt");

在这个示例中,getAssets()返回一个 AssetManager实例,然后您可以自由地使用 AssetManager API 中的任何方法。

试试这个,

package example.txtRead;


import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;


import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class txtRead extends Activity {
String labels="caption";
String text="";
String[] s;
private Vector<String> wordss;
int j=0;
private StringTokenizer tokenizer;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wordss = new Vector<String>();
TextView helloTxt = (TextView)findViewById(R.id.hellotxt);
helloTxt.setText(readTxt());
}


private String readTxt(){


InputStream inputStream = getResources().openRawResource(R.raw.toc);
//     InputStream inputStream = getResources().openRawResource(R.raw.internals);
System.out.println(inputStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();


int i;
try {
i = inputStream.read();
while (i != -1)
{
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


return byteArrayOutputStream.toString();
}
}

在 Mono For Android 中..。

try
{
System.IO.Stream StrIn = this.Assets.Open("MyMessage.txt");
string Content = string.Empty;
using (System.IO.StreamReader StrRead = new System.IO.StreamReader(StrIn))
{
try
{
Content = StrRead.ReadToEnd();
StrRead.Close();
}
catch (Exception ex) { csFunciones.MostarMsg(this, ex.Message); }
}
StrIn.Close();
StrIn = null;
}
catch (Exception ex) { csFunciones.MostarMsg(this, ex.Message); }

读取保存在资产文件夹中的文件

public static String readFromFile(Context context, String file) {
try {
InputStream is = context.getAssets().open(file);
int size = is.available();
byte buffer[] = new byte[size];
is.read(buffer);
is.close();
return new String(buffer);
} catch (Exception e) {
e.printStackTrace();
return "" ;
}
}

下面是一个同时处理 rawasset文件的简单类:

公共类 ReadFromFile {

public static String raw(Context context, @RawRes int id) {
InputStream is = context.getResources().openRawResource(id);
int size = 0;
try {
size = is.available();
} catch (IOException e) {
e.printStackTrace();
return "";
}
return readFile(size, is);
}


public static String asset(Context context, String fileName) {
InputStream is = null;
int size = 0;
try {
is = context.getAssets().open(fileName);
AssetFileDescriptor fd = null;
fd = context.getAssets().openFd(fileName);
size = (int) fd.getLength();
fd.close();
} catch (IOException e) {
e.printStackTrace();
return "";
}
return readFile(size, is);
}




private static String readFile(int size, InputStream is) {
try {
byte buffer[] = new byte[size];
is.read(buffer);
is.close();
return new String(buffer);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}

}

例如:

ReadFromFile.raw(context, R.raw.textfile);

至于资产档案:

ReadFromFile.asset(context, "file.txt");