如何在android中(永远)保存一个文件,并在以后恢复数据?
How to save (forever) a file in android, and restore the data later?
我从字面上查看了 Whosebug,试图找到正确的答案...也许我做错了什么。
我正在制作我的第一个更精致的应用程序,即儿童测验应用程序。我希望将分数保存在 highscorestable.txt 中,稍后将打开、更新、读取等。关闭应用程序后该文件应该存在,以便在下一场比赛中重用它等等。
我正在使用 http://developer.android.com/guide/topics/data/data-storage.html 。我希望将文件保存在 phone 内存中。
我有以下代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_high_scores);
Intent intent = getIntent();
String userData;
scores =(EditText)findViewById(R.id.scores);
if(intent.getStringExtra(EndScreen.EXTRA_MESSAGE)!=null)
{
userData = intent.getStringExtra(EndScreen.EXTRA_MESSAGE);
}
else
{
userData="";
}
String temp = "";//to widzi przy zaladowaniu
String output = "";
String g="";
//FIRST READING not necessary?
try{
FileInputStream fin = openFileInput("highscorestable.txt");
int c;
while( (c = fin.read()) != -1){
temp = temp + Character.toString((char)c);
}
fin.close();
}
catch (Exception e) {
// e.printStackTrace();
}
if(temp.equals(null))
{
temp = "";
}
//output = userData; //+ temp;
FileOutputStream fos;//WRITING
try {
fos = openFileOutput("highscorestable.txt", Context.MODE_PRIVATE);
fos.write(userData.getBytes());
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
//READING2
try{
FileInputStream fin = openFileInput("highscorestable.txt");
int c;
while( (c = fin.read()) != -1){
g = g + Character.toString((char)c);
}
fin.close();
output= output+g;
scores.setText(output);
}
catch (Exception e) {
}
意图本身有效,实际上一切正常,但文件本身不会持久。我的意思是,当我开始新游戏时,旧数据不会恢复。如何解决?
尝试使用 sharedPrefrences:http://developer.android.com/training/basics/data-storage/shared-preferences.html
它们以 XML 的形式永久存储在应用程序中,并且只有在正确配置的情况下才能被应用程序读取。 (除非您需要文本,请阅读此处:http://developer.android.com/training/basics/data-storage/files.html。请记住将权限添加到您的清单中)
编辑:
尝试追加到不写
的文件
我从字面上查看了 Whosebug,试图找到正确的答案...也许我做错了什么。 我正在制作我的第一个更精致的应用程序,即儿童测验应用程序。我希望将分数保存在 highscorestable.txt 中,稍后将打开、更新、读取等。关闭应用程序后该文件应该存在,以便在下一场比赛中重用它等等。 我正在使用 http://developer.android.com/guide/topics/data/data-storage.html 。我希望将文件保存在 phone 内存中。 我有以下代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_high_scores);
Intent intent = getIntent();
String userData;
scores =(EditText)findViewById(R.id.scores);
if(intent.getStringExtra(EndScreen.EXTRA_MESSAGE)!=null)
{
userData = intent.getStringExtra(EndScreen.EXTRA_MESSAGE);
}
else
{
userData="";
}
String temp = "";//to widzi przy zaladowaniu
String output = "";
String g="";
//FIRST READING not necessary?
try{
FileInputStream fin = openFileInput("highscorestable.txt");
int c;
while( (c = fin.read()) != -1){
temp = temp + Character.toString((char)c);
}
fin.close();
}
catch (Exception e) {
// e.printStackTrace();
}
if(temp.equals(null))
{
temp = "";
}
//output = userData; //+ temp;
FileOutputStream fos;//WRITING
try {
fos = openFileOutput("highscorestable.txt", Context.MODE_PRIVATE);
fos.write(userData.getBytes());
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
//READING2
try{
FileInputStream fin = openFileInput("highscorestable.txt");
int c;
while( (c = fin.read()) != -1){
g = g + Character.toString((char)c);
}
fin.close();
output= output+g;
scores.setText(output);
}
catch (Exception e) {
}
意图本身有效,实际上一切正常,但文件本身不会持久。我的意思是,当我开始新游戏时,旧数据不会恢复。如何解决?
尝试使用 sharedPrefrences:http://developer.android.com/training/basics/data-storage/shared-preferences.html
它们以 XML 的形式永久存储在应用程序中,并且只有在正确配置的情况下才能被应用程序读取。 (除非您需要文本,请阅读此处:http://developer.android.com/training/basics/data-storage/files.html。请记住将权限添加到您的清单中)
编辑: 尝试追加到不写
的文件