开发者

Problems writing then reading from file in Android

I have a problem with my code. Basically I'm trying to save 5 strings to a file, and then when the class opens read the txt file and recover the 5 strings. However I only recover one, and not the 5. Am I saving the code incorrectly?

public class Activity2 extends Activity {
/** Called when the activity is first created. */
boolean checkTick = false;
Activity1 one;
Boolean [] Checkboxs = {false,false,false,false,false};
String [] Storetype = {"","","","",""};
ArrayList<Boolean> bList = new ArrayList<Boolean>(); 
ArrayList<String> sList = new ArrayList<String>(); 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings);

    readFile();

    final Button buttonHOME = (Button) findViewById(R.id.widget900);         
    buttonHOME.setOnClickListener(new ViewStub.OnClickListener() {             
     @Override
     public void onClick(View view) {
      Intent myIntent = new Intent(view.getContext(), Activity1.class);
            startActivityForResult(myIntent, 0);
            finish();
        }

     });
    final Button buttonEXIT = (Button) findViewById(R.id.widget41);         
    buttonEXIT.setOnClickListener(new ViewStub.OnClickListener() {             
     @Override
    public void onClick(View arg0) {
          System.exit(0);


       }         });

    final CheckBox checkBox =
       ( CheckBox ) findViewById( R.id.widget36 );
          checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
           {


        @Override
           public void onCheckedChanged(CompoundButton arg0,boolean arg1) {
           if(checkBox.isChecked()){
       // TODO Auto-generated method stub
          checkTick = true;
               Checkboxs[0] = true;

               storeValue();
                 System.out.println("newthing" + checkTick);
 }
               if(!checkBox.isChecked()){
             checkTick = false;
  Checkboxs[0] = false;
  storeValue();
 }
}
             });

            if(checkTick){
            //if(Checkboxs[0])
              checkBox.setChecked(checkTick);
                 }

     }

public void storeValue(){

  String storeType = "";

  for(int i = 0; i < 5; i++){
      if(Checkboxs[i]){
         Storetype[i] = "true";
   }
   else{
       Storetype[i] = "false";
   }
  }
  //if(checkTick){
  // storeType = "true";

 // }
 // else{
 //  storeType = "false";
 //  }

  try{


  FileOutputStream fOut = openFileOutput("samplefile.txt", 
                MODE_WORLD_READABLE);
  OutputStreamWriter osw = new OutputStreamWriter(fOut); 

  // Write the string to the file
  for(int i = 0; i < 5; i++){
   osw.write(Storetype[i] + "\n");
  }

  //osw.write(storeType);
  /* ensure that everything is 
   * really written out and close */

  osw.flush();
  osw.close();
  System.out.println("The value is now stored in a file" + storeType);
  }catch (IOException ioe){
   ioe.printStackTrace();
  }
  System.out.println("The value is now stored" + storeType);
 }
public void readFile(){
 try{
        String datahold = "";

        FileInputStream fIn = openFileInput("samplefile.txt");
        DataInputStream isr = new DataInputStream(fIn);
        BufferedReader br = new BufferedReader(new InputStreamReader(isr));


        // Transform the chars to a String
       // if(datahold.equals("true")){
        // checkTick = true;

      //  }
      //  else{
        // checkTick = false;
       // }
        while((dat开发者_运维知识库ahold = br.readLine()) != null){
         sList.add(datahold);
        }
        for(int i = 0; i < sList.size(); i++){
         System.out.println("ARRAY" + sList.get(i));
        }

 } catch (IOException ioe) {
        ioe.printStackTrace();
 }


}
     public void autoSign(boolean checkTickTwo){
     checkTick = checkTickTwo;
    }




}


Because that's OSWs default behaviour. Use BufferedWriter instead.


You should append the String first then write it once. Something like


FileOutputStream fOut = openFileOutput("samplefile.txt", 
        MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut); 
StringBuffer buffer = new StringBuffer();

// Write the string to the file
for(int i = 0; i < 5; i++){
    buffer.append(Storetype[i] + "\n");
}

osw.write(buffer.toString());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜