Android 中的文件未找到消息 toast 异常
File not found exception in Android for message toast
我实际上是在尝试在存储在 Android 应用程序资产文件夹中的文本文件中搜索给定字符串。我写的代码是:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button;
final EditText obedittext;
button =(Button)findViewById(R.id.button1);
obedittext =(EditText)findViewById(R.id.editText1);
button.setOnClickListener(
new View.OnClickListener()
{
boolean textfound;
public void onClick(View view)
{
textfound = searchtext(obedittext.getText().toString());
if(textfound)
maketoast(obedittext.getText().toString());
else
maketoast("Unsuccessfull");
}
});
}
protected boolean searchtext(String string) {
// TODO Auto-generated method stub
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("mneumo.txt"));
while ((sCurrentLine = br.readLine()) != null) {
if(sCurrentLine.equals(string)) {
return true;
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
finally{
}
return false;
}
private void maketoast(String string) {
// TODO Auto-generated method stub
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, string , Toast.LENGTH_SHORT);
toast.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我收到的错误是:
03-06 01:17:01.330: W/System.err(1170): java.io.FileNotFoundException: /mneumo.txt: open failed: ENOENT (No such file or directory)
03-06 01:17:01.330: W/System.err(1170): at libcore.io.IoBridge.open(IoBridge.java:409)
03-06 01:17:01.330: W/System.err(1170): at java.io.FileInputStream.<init>(FileInputStream.java:78)
03-06 01:17:01.340: W/System.err(1170): at java.io.FileInputStream.<init>(FileInputStream.java:105)
03-06 01:17:01.340: W/System.err(1170): at java.io.FileReader.<init>(FileReader.java:66)
03-06 01:17:01.340: W/System.err(1170): at com.example.demo.MainActivity.searchtext(MainActivity.java:60)
03-06 01:17:01.340: W/System.err(1170): at com.example.demo.MainActivity.onClick(MainActivity.java:41)
示例文件是,
SPINAL ANESTHESIA AGENTS
XYLOCAINE: WHERE NOT TO USE WITH EPINEPHRINE
GENERAL ANAESTHESIA: EQUIPMENT CHECK PRIOR TO INDUCING
如果找到该字符串,则应该显示带有该字符串的祝酒词。但它总是说 "file not found"。而且我完全是新手。
这是一个类似于字典的应用程序。
我确实参考了该站点中的其他问题,但我仍然无法弄清楚问题出在哪里。我应该使用 assetmanager 还是其他东西?
您想从资产中读取,对吗?您需要使用 AssetManager
来完成它。见下文:
InputStream is;
try {
is = getAssets().open("myfile.txt");
byte[] buffer = new byte[is.available()];
is.read(buffer);
is.close();
Toast.makeText(this, new String(buffer, "UTF-8"), Toast.LENGTH_SHORT).show();
} catch (IOException ex) {
ex.printStackTrace();
}
当您从 Asset 文件夹中读取文件时,请尝试以下代码:
BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open("mneumo.txt")));
// do reading, usually loop until end of file reading
String mLine = reader.readLine();
while (mLine != null) {
//process line
...
mLine = reader.readLine();
}
} catch (IOException e) {
//log the exception
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
//log the exception
}
}
}
你的代码声明
br = new BufferedReader(new FileReader("mneumo.txt"));
指的是错误的位置。因此,它无法读取文件。相反,通过以下
替换上面的行
br = new BufferedReader(new InputStreamReader(getAssets().open("mneumo.txt")));
这样,应该可以找到并打开您的文件。存储在应用程序 asset 文件夹中的文件应始终以这种方式读取,而不是通过硬编码目录字符串或应用程序相对路径读取。
我实际上是在尝试在存储在 Android 应用程序资产文件夹中的文本文件中搜索给定字符串。我写的代码是:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button;
final EditText obedittext;
button =(Button)findViewById(R.id.button1);
obedittext =(EditText)findViewById(R.id.editText1);
button.setOnClickListener(
new View.OnClickListener()
{
boolean textfound;
public void onClick(View view)
{
textfound = searchtext(obedittext.getText().toString());
if(textfound)
maketoast(obedittext.getText().toString());
else
maketoast("Unsuccessfull");
}
});
}
protected boolean searchtext(String string) {
// TODO Auto-generated method stub
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("mneumo.txt"));
while ((sCurrentLine = br.readLine()) != null) {
if(sCurrentLine.equals(string)) {
return true;
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
finally{
}
return false;
}
private void maketoast(String string) {
// TODO Auto-generated method stub
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, string , Toast.LENGTH_SHORT);
toast.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我收到的错误是:
03-06 01:17:01.330: W/System.err(1170): java.io.FileNotFoundException: /mneumo.txt: open failed: ENOENT (No such file or directory)
03-06 01:17:01.330: W/System.err(1170): at libcore.io.IoBridge.open(IoBridge.java:409)
03-06 01:17:01.330: W/System.err(1170): at java.io.FileInputStream.<init>(FileInputStream.java:78)
03-06 01:17:01.340: W/System.err(1170): at java.io.FileInputStream.<init>(FileInputStream.java:105)
03-06 01:17:01.340: W/System.err(1170): at java.io.FileReader.<init>(FileReader.java:66)
03-06 01:17:01.340: W/System.err(1170): at com.example.demo.MainActivity.searchtext(MainActivity.java:60)
03-06 01:17:01.340: W/System.err(1170): at com.example.demo.MainActivity.onClick(MainActivity.java:41)
示例文件是,
SPINAL ANESTHESIA AGENTS
XYLOCAINE: WHERE NOT TO USE WITH EPINEPHRINE
GENERAL ANAESTHESIA: EQUIPMENT CHECK PRIOR TO INDUCING
如果找到该字符串,则应该显示带有该字符串的祝酒词。但它总是说 "file not found"。而且我完全是新手。
这是一个类似于字典的应用程序。
我确实参考了该站点中的其他问题,但我仍然无法弄清楚问题出在哪里。我应该使用 assetmanager 还是其他东西?
您想从资产中读取,对吗?您需要使用 AssetManager
来完成它。见下文:
InputStream is;
try {
is = getAssets().open("myfile.txt");
byte[] buffer = new byte[is.available()];
is.read(buffer);
is.close();
Toast.makeText(this, new String(buffer, "UTF-8"), Toast.LENGTH_SHORT).show();
} catch (IOException ex) {
ex.printStackTrace();
}
当您从 Asset 文件夹中读取文件时,请尝试以下代码:
BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open("mneumo.txt")));
// do reading, usually loop until end of file reading
String mLine = reader.readLine();
while (mLine != null) {
//process line
...
mLine = reader.readLine();
}
} catch (IOException e) {
//log the exception
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
//log the exception
}
}
}
你的代码声明
br = new BufferedReader(new FileReader("mneumo.txt"));
指的是错误的位置。因此,它无法读取文件。相反,通过以下
替换上面的行br = new BufferedReader(new InputStreamReader(getAssets().open("mneumo.txt")));
这样,应该可以找到并打开您的文件。存储在应用程序 asset 文件夹中的文件应始终以这种方式读取,而不是通过硬编码目录字符串或应用程序相对路径读取。