Java 如何在Android中读取文本文件?
我想从文本文件中读取文本。在下面的代码中,发生异常(这意味着它进入Java 如何在Android中读取文本文件?,java,android,exception,inputstream,Java,Android,Exception,Inputstream,我想从文本文件中读取文本。在下面的代码中,发生异常(这意味着它进入catch块)。我将文本文件放在应用程序文件夹中。我应该把这个文本文件(mani.txt)放在哪里才能正确阅读 try { InputStream instream = openFileInput("E:\\test\\src\\com\\test\\mani.txt"); if (instream != null) { InputStrea
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();
}
试试这个:
我假设你的文本文件在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());
以下链接也可以帮助您:
将文本文件放入资产文件夹中…&从该文件夹中读取文件 请参阅下面的参考链接
如果您想从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.
}
}
如果您想从资产文件夹中读取文件,则
AssetManager am = context.getAssets();
InputStream is = am.open("test.txt");
或者,如果您想从res/raw
foldery中读取此文件,则该文件将被编入索引并可通过R文件中的id访问:
InputStream is = getResources().openRawResource(R.raw.test);
首先将文本文件存储到原始文件夹中
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.");
}
private void loadWords()引发IOException{
Log.d(标签“加载单词…”);
final Resources Resources=mhelpocontext.getResources();
InputStream InputStream=resources.openRawResource(R.raw.definitions);
BufferedReader reader=新的BufferedReader(新的InputStreamReader(inputStream));
试一试{
弦线;
而((line=reader.readLine())!=null){
String[]strings=TextUtils.split(第“-”行);
如果(字符串长度<2)
继续;
long id=addWord(字符串[0].trim(),字符串[1].trim());
if(id<0){
Log.e(标记“无法添加单词:”+strings[0].trim());
}
}
}最后{
reader.close();
}
Log.d(标记“完成加载单词”);
}
试试这个
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);
}
试试这个代码
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;
}
小型文本文件的最短格式(Kotlin):
您希望模拟器成为s/m的一部分是什么?“E:\\test\\src\\com\\test\\mani.txt”要从哪个位置读取文本文件?InputStream iS=resources.getAssets().open(文件名);(如果你把文件放在assets中)@Sandip实际上我复制了文本文件(mani.txt)并把它放在android应用程序的文件夹中(文件夹有.settings、bin、libs、src、assets、gen、res、androidmanifeat.xml),或者放在简单的res/raw文件夹中并检查我的更新答案。你的链接将帮助我实现BufferedReader需要在最后关闭!如果您的txt文档中有一个空行,该解析器将停止工作!解决方案是承认有这样的空行:
while((line=br.readLine())!=null){if(line.length()>0){//do your stuff}}
@Shruti如何将文件添加到SD中card@Choletski你为什么说它会停止工作?如果有空行,空白行将附加到文本StringBuilder。有什么问题?br
在finally块中超出范围。
val reader = FileReader(path)
val txt = reader.readText()
reader.close()