gpt4 book ai didi

android - 文件未保存在 Android 中

转载 作者:行者123 更新时间:2023-11-30 04:36:28 25 4
gpt4 key购买 nike

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

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("");
}
}
}

最佳答案

您可能正在读取文件最后一行的\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 打开。

关于android - 文件未保存在 Android 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6748225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com