文件未保存在Android中

我正在尝试创建一个简单的Android应用程序,该应用程序从EditText表单保存文本并将其存储在内部存储器中。 一切似乎工作正常(“保存”消息出现),除非我退出活动并重新启动它时,从文件保存的文本根本不加载。 我在这里错过了什么吗?

public class ModifyInfo extends Activity{

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit);

    Bundle extras = getIntent().getExtras(); 
    if(extras != null){
        int dayNum = extras.getInt("day");
        String dayName = "";

        switch(dayNum){
        case 1:
            dayName = this.getString(R.string.dayMon);
            break;
        case 2:
            dayName = this.getString(R.string.dayTue);
            break;
        case 3:
            dayName = this.getString(R.string.dayWed);
            break;
        case 4:
            dayName = this.getString(R.string.dayThu);
            break;
        case 5:
            dayName = this.getString(R.string.dayFri);
            break;
        case 6:
            dayName = this.getString(R.string.daySat);
            break;
        default:
            dayName = this.getString(R.string.daySun);
            break;
        }

        final TextView dayText = (TextView) findViewById(R.id.dayName);
        final TextView editData = (TextView) findViewById(R.id.editData);
        final Button save = (Button) findViewById(R.id.buttonSave);
        final Button clear = (Button) findViewById(R.id.buttonClear);
        final String Day = dayName;

        dayText.setText(dayName);

        clear.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editData.setText("");
            }
        });

        save.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String FILEOUTPUT = Day + ".txt";

                try {
                    String string = editData.getText().toString();

                    FileOutputStream fos = openFileOutput(FILEOUTPUT, Context.MODE_PRIVATE);
                    fos.write(string.getBytes());
                    fos.close();

                    Toast.makeText(ModifyInfo.this, "Saved", Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    Toast.makeText(ModifyInfo.this, "Save error", Toast.LENGTH_SHORT).show();   
                }
            }
        });

        File FILEINPUT = new File(Day + ".txt");

        try {
            BufferedReader bfr = new BufferedReader(new FileReader(FILEINPUT));
            String line;

            while ((line = bfr.readLine()) != null)
            {
                editData.setText(line);
            }

            bfr.close();

        } catch (Exception e) {
            editData.setText("");
        }   
    }   
}

}


Android可能会锁定某些文件夹中的文件,并且无法修改它们。 您可以使用sharedpreferences或静态对象。


由于您正在调用editData.setText(line);您可能正在读取 n或文件最后一行的空格。 ,将文本重置为当前行。 或者您也可能遇到异常,在这种情况下,您通过清除editText来处理该异常。

它应该是这样的:

try {
        BufferedReader bfr = new BufferedReader(new FileReader(FILEINPUT));
        String line;
        String fullText = "";

        while ((line = bfr.readLine()) != null)
        {
            fullText += line;
        }

        bfr.close();

        editData.setText(fullText);

    } catch (Exception e) {
        editData.setText("");
    }   

根据您的说明,您应该检查您是否按照以下说明正确打开了您的文件:http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

从内部存储读取文件:

1. Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream.
2. Read bytes from the file with read().
3. Then close the stream with close().

您正尝试使用BufferedReader代替。

链接地址: http://www.djcxy.com/p/90009.html

上一篇: File not saved in Android

下一篇: How to create and write to xml file in android