我编写了以下代码。但是当我按下按钮并重新启动程序时,我再次看到There is no the file.xml file
消息。
File file = new File("/data/data/" + getPackageName() + "/shared_prefs/" + getPackageName()+ "file.xml");
if (file.exists())
Toast.makeText(getApplicationContext(), "There is the file.xml file", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(),"There is no the file.xml file",Toast.LENGTH_SHORT).show();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sharedpreferences;
sharedpreferences = getApplicationContext().getSharedPreferences("file", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("file","I am a file");
editor.commit();
}
});
答案 0 :(得分:0)
if (file.exists())
Toast.makeText(getApplicationContext(), "There is the file.xml file", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(),"There is no the file.xml file",Toast.LENGTH_SHORT).show();
you can test the code in you project.
that "There is no the file.xml file" is a message. you can change it.
if(file.exists()){
Toast.makeText(getApplicationContext(), "There is not the file.xml file", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "There is the file.xml file", Toast.LENGTH_SHORT).show();
}