【发布时间】:2015-10-20 11:19:42
【问题描述】:
我在检查文件是否存在时遇到问题,这是我的简单代码:
File myfile = new File ("SecretFile");
if(myfile.exists(){
TextView mytxt = (TextView)findViewById(R.id.textView);
mytxt.setText("Loaded successfully");
}
else{
try {
myfile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
TextView mytxt = (TextView)findViewById(R.id.textView);
mytxt.setText("Not loaded,reboot the application");
}
我正在做一个简单的游戏,我需要检查第一个应用程序运行,因为当应用程序运行时,它会从文件中加载所有变量,但在第一次运行时没有文件,因此应用程序崩溃。 我还有另一个问题文件“SecretFile”在哪里? 如何在 Android/data/com.mypackage.myapp/SecretFile 中创建文件?
【问题讨论】:
-
添加此权限
android.permission.WRITE_EXTERNAL_STORAGE
标签: java android file-exists