我的代码有问题。基本上我正在尝试将5个字符串保存到文件中,然后当类打开时读取txt文件并恢复5个字符串。但是我只收回一个,而不是5.我是否错误地保存了代码?
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 :(得分:0)
因为那是OSW的默认行为。改为使用BufferedWriter。
答案 1 :(得分:0)
您应首先附加String然后再写一次。像
这样的东西
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());