【问题标题】:File not saved in Android文件未保存在 Android 中
【发布时间】:2023-03-15 18:42:02
【问题描述】:

我正在尝试创建一个简单的 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("");
        }   
    }   
}

}

【问题讨论】:

  • 我会改进异常处理。一般来说,捕获 Exception 类的实例并不是一个好习惯。您应该更好地记录异常,以便您知道它们何时被抛出。

标签: android file save


【解决方案1】:

您可能正在从文件的最后一行读取 \n 或类似的空格,因为您正在调用 editData.setText(line); ,将文本重置为当前行。 或者您也可能遇到异常,在这种情况下,您正在通过清除 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 打开。

【讨论】:

  • 它不断返回一个找不到文件的异常,这意味着该文件没有正确存储,如果有的话。我的 save.setOnClickListener 有什么问题吗?
  • 我修改了我的答案。你能检查一下 openFileInput() 吗?
  • 非常感谢您的回复,但我设法使用 SharedPreferences 修复它,结果证明它要简单得多。很抱歉给您带来麻烦。
【解决方案2】:

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

【讨论】:

  • 我必须设置一些权限吗?
  • 我无法设置它。从 os 查看文件的属性。
猜你喜欢
  • 2022-11-18
  • 2015-12-17
  • 2019-05-04
  • 1970-01-01
  • 2021-11-23
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多