android.content.res.Resources $ NotFoundException

时间:2014-06-10 10:19:06

标签: java android eclipse file exception

嘿我在从txt文件加载数据时遇到问题

private void read() {


          try {
                // open the file for reading
                InputStream instream = openFileInput(File.separator + "bla.txt");


            // if file the available for reading
            if (instream.available() >0) {
              // prepare the file for reading
              InputStreamReader inputreader = new InputStreamReader(instream);
              BufferedReader buffreader = new BufferedReader(inputreader);

              String line;

              // read every line of the file into the line-variable, on line at the time
              while (( line = buffreader.readLine()) != null) {
                  for(int i= 0; i<=1; i++){
                        if(i == 0){
                            bla = Integer.parseInt(buffreader.readLine());
                        }
                        if(i == 1){
                            bla2 = Integer.parseInt(buffreader.readLine());
                        }


                  }

            // close the file again      
            instream.close();


            Toast.makeText(getBaseContext(),
                    "Ladevorgang war efolgreich!",

                    Toast.LENGTH_LONG).show();

              }}
          }

               catch (java.io.FileNotFoundException e) {
                   Toast.makeText(getBaseContext(),
                            "Datei nicht vorhanden",

                            Toast.LENGTH_LONG).show();
                  }
         catch (Exception e) {
            Toast.makeText(getBaseContext(), e.getMessage(),
                    Toast.LENGTH_SHORT).show();

请帮帮我,我不知道应该改变什么:( 我总是得到android.content.res.Resources $ NotFoundException:字符串资源ID#0x0 感谢Strik3r的帮助

private  void save() {

    try {

        File myFile = new File(Environment.getExternalStorageDirectory() + File.separator + "bla.txt");
        myFile.createNewFile();
        FileOutputStream fOut = new FileOutputStream(myFile);
        OutputStreamWriter myOutWriter = 
                                new OutputStreamWriter(fOut);
        myOutWriter.append(string.toString() + "\n");
        myOutWriter.append(string2.toString());

        myOutWriter.close();
        fOut.close();
        Toast.makeText(getBaseContext(),
                "Gespeichert",
                Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        Toast.makeText(getBaseContext(), e.getMessage(),
                Toast.LENGTH_SHORT).show();
    }
}

这是保存数据

1 个答案:

答案 0 :(得分:2)

 // open the file for reading

    InputStream instream = openFileInput(File.separator + "bla.txt");

似乎不是有效的文件。 相反,请尝试

InputStream instream = openFileInput(Environment.getExternalStorageDirectory() +File.separator + "bla.txt");