[英]File not found exception in Android for message toast
我實際上是想在我存儲在Android應用程序內Assets文件夾中的文本文件中搜索給定的字符串。 我寫的代碼是:
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$1.onClick(MainActivity.java:41)
示例文件是
SPINAL ANESTHESIA AGENTS
XYLOCAINE: WHERE NOT TO USE WITH EPINEPHRINE
GENERAL ANAESTHESIA: EQUIPMENT CHECK PRIOR TO INDUCING
如果找到該字符串,則應該顯示該字符串的吐司。 但是它總是說“找不到文件”。 我是新手。
這適用於類似於字典的應用程序。
我確實在該站點上還提到了其他問題,但是我仍然無法弄清楚問題出在哪里。 我應該使用資產經理還是其他?
您的代碼聲明
br = new BufferedReader(new FileReader("mneumo.txt"));
指錯誤的位置。 因此,它無法讀取文件。 相反,請按照以下內容替換上面的行
br = new BufferedReader(new InputStreamReader(getAssets().open("mneumo.txt")));
這樣,應該找到並打開您的文件。 存儲在應用程序資產文件夾中的文件應始終以這種方式讀取,而不是通過硬編碼的目錄字符串或應用程序相對路徑讀取。
您想閱讀資產,對嗎? 您需要使用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();
}
從資產文件夾讀取文件時,請嘗試以下代碼:
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
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.