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;
}
}
行,所以我想我的問題是這對(INT I = 0; I <5;我++){ osw.write(的storetype [I] + 「\ n」); }它似乎只寫一個字符串到文件,任何人都知道爲什麼? –