2011-09-05 86 views
0

我的代码有问题。基本上我试图将5个字符串保存到一个文件中,然后当类打开时读取txt文件并恢复5个字符串。但是,我只能恢复一个,而不是5.我是否错误地保存了代码?从Android中的文件读取然后读取的问题

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((datahold = 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; 
    } 




} 
+0

行,所以我想我的问题是这对(INT I = 0; I <5;我++){ osw.write(的storetype [I] + “\ n”); }它似乎只写一个字符串到文件,任何人都知道为什么? –

回答

0

因为这是OSW的默认行为。改用BufferedWriter。

0

您应该首先追加字符串,然后写入一次。像


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());