本人最近在做一个在android实现sim卡卡号绑定功能的实验,我是将绑定时用到的匹配文件存储在SD卡下,当我操作程序界面中的某一按钮控件后,可向相应的匹配文件中写入数据,但是我通过使用另外的一个按钮控件向文件写入其他内容后,在我重启系统后,我发现android模拟器下的文件中的内容全部为空,什么内容都没有了,请问一下大家这是什么原因呢?以下是我的部分操作代码,请大家指教。 Buttonrewrite.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
DataOutputStream dataout = null;
FileOutputStream fout = null;
// String data = "1354";
try
{
fout = new FileOutputStream("\\sdcard\\data.dat");
dataout = new DataOutputStream(fout);
try
{
Thread.sleep(1000);
}
catch(Exception ex)
{
}
dataout.writeBytes(imsi);
dataout.close();
fout.close();
}
catch(Exception ex)
{
Toast.makeText(Banding.this, "重新写入有错误", Toast.LENGTH_LONG).show();
}
AlertDialog.Builder Rewrite= new AlertDialog.Builder(Banding.this);
Rewrite.setTitle("关于重新写入号码")
.setMessage("重新写入成功")
.show();
}
});
ButtonSet.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
String config = "open";
FileOutputStream matchfile = null;
DataOutputStream dataout = null;
try
{
matchfile = new FileOutputStream("\\sdcard\\matchfile.dat");
dataout = new DataOutputStream(matchfile);
try
{
Thread.sleep(1000);
}
catch(Exception ex)
{
}
dataout.writeBytes(config); dataout.close();
matchfile.close();
}
catch(Exception ex)
{
Toast.makeText(Banding.this, "设置关闭错误", Toast.LENGTH_LONG).show();
}
AlertDialog.Builder DialogSet = new AlertDialog.Builder(Banding.this);
DialogSet.setTitle("关于绑定SIM卡")
.setMessage("开启绑定功能")
.show();
}
});
ButtonCancel.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
String config = "close";
FileOutputStream matchfile = null;
DataOutputStream dataout = null;
try
{
matchfile = new FileOutputStream("\\sdcard\\matchfile.dat");
dataout = new DataOutputStream(matchfile);
try
{
Thread.sleep(1000);
}
catch(Exception ex)
{
}
dataout.writeBytes(config);
dataout.close();
matchfile.close();
}
catch(Exception ex)
{
Toast.makeText(Banding.this, "设置开启错误", Toast.LENGTH_LONG).show();
}
AlertDialog.Builder DialogCancel = new AlertDialog.Builder(Banding.this);
DialogCancel.setTitle("关于绑定SIM卡")
.setMessage("关闭绑定功能")
.show();
}
});
{
public void onClick(View v)
{
DataOutputStream dataout = null;
FileOutputStream fout = null;
// String data = "1354";
try
{
fout = new FileOutputStream("\\sdcard\\data.dat");
dataout = new DataOutputStream(fout);
try
{
Thread.sleep(1000);
}
catch(Exception ex)
{
}
dataout.writeBytes(imsi);
dataout.close();
fout.close();
}
catch(Exception ex)
{
Toast.makeText(Banding.this, "重新写入有错误", Toast.LENGTH_LONG).show();
}
AlertDialog.Builder Rewrite= new AlertDialog.Builder(Banding.this);
Rewrite.setTitle("关于重新写入号码")
.setMessage("重新写入成功")
.show();
}
});
ButtonSet.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
String config = "open";
FileOutputStream matchfile = null;
DataOutputStream dataout = null;
try
{
matchfile = new FileOutputStream("\\sdcard\\matchfile.dat");
dataout = new DataOutputStream(matchfile);
try
{
Thread.sleep(1000);
}
catch(Exception ex)
{
}
dataout.writeBytes(config); dataout.close();
matchfile.close();
}
catch(Exception ex)
{
Toast.makeText(Banding.this, "设置关闭错误", Toast.LENGTH_LONG).show();
}
AlertDialog.Builder DialogSet = new AlertDialog.Builder(Banding.this);
DialogSet.setTitle("关于绑定SIM卡")
.setMessage("开启绑定功能")
.show();
}
});
ButtonCancel.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
String config = "close";
FileOutputStream matchfile = null;
DataOutputStream dataout = null;
try
{
matchfile = new FileOutputStream("\\sdcard\\matchfile.dat");
dataout = new DataOutputStream(matchfile);
try
{
Thread.sleep(1000);
}
catch(Exception ex)
{
}
dataout.writeBytes(config);
dataout.close();
matchfile.close();
}
catch(Exception ex)
{
Toast.makeText(Banding.this, "设置开启错误", Toast.LENGTH_LONG).show();
}
AlertDialog.Builder DialogCancel = new AlertDialog.Builder(Banding.this);
DialogCancel.setTitle("关于绑定SIM卡")
.setMessage("关闭绑定功能")
.show();
}
});
dataout.close();
另外,按照帮助文档的解释,flush()方法并不是必须的吧;