[英]Creating, reading & saving a file on internal storage (Android)
I'm creating a little game in Android, now I want to use a file as a savegame. 我正在用Android创建一个小游戏,现在我想将文件用作保存游戏。 On Start it checks if there is already a savegame, if not, it should create it. 在启动时,它会检查是否已经存在一个保存游戏,如果没有,则应该创建一个保存游戏。 If there is a savegame it should read it. 如果有保存游戏,则应阅读该游戏。 And when you close the app/turn your phone off/etc. 而当您关闭应用程序/关闭手机等时。 it should save it. 它应该保存它。 To code this, i googled a bit and found something, but it seems that it doesn't work, but I don't know why. 为了对此进行编码,我在谷歌上搜索了一下,发现了一些东西,但似乎不起作用,但我不知道为什么。 Here are the relevant snippets of my class: 以下是我班上的相关摘录:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
FileStart();
}
public void FileStart() {
File external = getFilesDir();
String path = external.getPath();
File file = new File(path + "inv.txt");
if(file.exists()) {
//read files
try {
BufferedReader in = new BufferedReader(new FileReader(file));
String result = in.readLine();
String[] resultSplit = result.split("|");
fruits = Integer.parseInt(resultSplit[0]);
milk = Integer.parseInt(resultSplit[1]);
sugar = Integer.parseInt(resultSplit[2]);
wheat = Integer.parseInt(resultSplit[3]);
cake = Integer.parseInt(resultSplit[4]);
in.close();
TextView tView = new TextView(this);
tView=(TextView)findViewById(R.id.textView1);
tView.setText(fruits + " x Fruits");
tView=(TextView)findViewById(R.id.textView3);
tView.setText(milk + " x Milk");
tView=(TextView)findViewById(R.id.textView4);
tView.setText(sugar + " x Sugar");
tView=(TextView)findViewById(R.id.textView5);
tView.setText(wheat + " x Wheat");
tView=(TextView)findViewById(R.id.textView9);
tView.setText(cake + " Cakes");
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {
file.createNewFile();
FileWriter filewriter = new FileWriter(file);
BufferedWriter out = new BufferedWriter(filewriter);
out.write("0|0|0|0|0");
out.flush();
out.close();
filewriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
protected void onPause () {
super.onPause(); // Always call the superclass method first
//save game progress
File external = getFilesDir();
String path = external.getPath();
File file = new File(path + "inv.txt");
try {
FileWriter filewriter = new FileWriter(file);
BufferedWriter out = new BufferedWriter(filewriter);
String toWrite = fruits + "|" + milk + "|" + "|" + sugar + "|" + wheat + "|" + cake;
out.write(toWrite);
out.flush();
out.close();
filewriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Use the following code to create a directory: 使用以下代码创建目录:
boolean success = (new File(getApplicationContext().getFilesDir()+"/YourFolderName")).mkdir();
if (!success){
Log.w("directory not created", "directory not created");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.