我实际上正在尝试在文本文件中搜索给定的字符串,该文本文件存储在我的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
如果找到字符串,则应该显示带有该字符串的祝酒词。但它总是说"找不到文件"。我完全是个新手。
这是一个类似于字典的应用程序。
我确实在这个网站上提到了其他问题,但我仍然不知道问题出在哪里。我应该使用assetmanager还是其他什么?
您的代码语句
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();
}