【问题标题】:File not found exception in Android for message toast在 Android 中找不到消息吐司的文件异常
【发布时间】:2015-03-06 06:37:18
【问题描述】:

我实际上是在尝试在我存储在我的 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$1.onClick(MainActivity.java:41)

示例文件是,

SPINAL ANESTHESIA AGENTS
XYLOCAINE: WHERE NOT TO USE WITH EPINEPHRINE
GENERAL ANAESTHESIA: EQUIPMENT CHECK PRIOR TO INDUCING

如果找到该字符串,则应该显示带有该字符串的 toast。但它总是说“找不到文件”。我完全是新手。

这适用于类似于字典的应用程序。

我确实参考了此站点中的其他问题,但我仍然无法弄清楚问题所在。我应该使用资产管理器还是其他什么?

【问题讨论】:

  • 很简单。确保 mneumo 是您项目中的当前目录。

标签: java android file search


【解决方案1】:

你的代码声明

br = new BufferedReader(new FileReader("mneumo.txt"));

指的是错误的位置。因此,它无法读取文件。相反,请按照以下方式替换上面的行

br = new BufferedReader(new InputStreamReader(getAssets().open("mneumo.txt")));

这样,您的文件应该会被找到并打开。存储在应用程序的asset 文件夹中的文件应始终以这种方式读取,而不是通过硬编码的目录字符串或应用程序相对路径。

【讨论】:

    【解决方案2】:

    您想从资产中读取数据,对吧?您需要使用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();
        }
    

    【讨论】:

      【解决方案3】:

      当您从 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
               }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-01-26
        • 1970-01-01
        • 2021-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-06
        • 1970-01-01
        • 2021-10-06
        相关资源
        最近更新 更多