本人最近在做一个在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();
         }
        });
       

解决方案 »

  1.   

    dataout.flush();
    dataout.close();
      

  2.   

    先看看重启之前有没有写进去吧,因为sdk1.6后写入SD卡时需要加入WRITE_EXTERNAL_STORAGE权限许可,否则不成功。
    另外,按照帮助文档的解释,flush()方法并不是必须的吧;
      

  3.   

    谢谢大家啊,我已经在XML文件中加入了那个sd卡的操作权限,但是还是不行,麻烦大家看一下那个程序哪里还有写的不完善的地方,另外,是不是需要找一个真机测试,是不是真机会要比模拟器好用一点,感觉那个模拟器好像不大好用啊!