我有一个需要读取和写入文本文件的应用程序。我有它读,但我没有它写。这个想法是,当我单击屏幕上的保存按钮时,它会将已放入 textviews 的所有信息保存到一个数组中,并将数组的每个段写入文本文件。这是我的写作部分代码:
public class AddOrModify extends Activity {
private Button Savebtn;
private Button Cancelbtn;
private EditText NameofRoute;
private EditText Address1;
private EditText City1;
private EditText State1;
private EditText Zip1;
private EditText Address2;
private EditText City2;
private EditText State2;
private EditText zip2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_or_modify);
Savebtn = (Button)findViewById(R.id.savebtn);
Savebtn.setOnClickListener(new btnlistenersave());
Cancelbtn = (Button)findViewById(R.id.cancelbtn);
Cancelbtn.setOnClickListener(new btnlistenercancel());
}
private class btnlistenersave implements View.OnClickListener{
public void onClick(View v) {
NameofRoute = (EditText)findViewById(R.id.NameofRoute);
Address1 = (EditText)findViewById(R.id.editAddress1);
City1 = (EditText)findViewById(R.id.City1);
State1= (EditText)findViewById(R.id.State1);
Zip1 = (EditText)findViewById(R.id.Zip1);
Address2= (EditText)findViewById(R.id.Address2);
City2 = (EditText)findViewById(R.id.City2);
State2 = (EditText)findViewById(R.id.State2);
zip2 = (EditText)findViewById(R.id.Zip2);
//String[] mySettings ={NameofRouteinput,Address1input,City1input, State1input,Zip1input,Address2input,City2input,State2input,Zip2input,";"};
// if(mySettings != null){
try{
String NameofRouteinput = NameofRoute.getText().toString();
String Address1input = Address1.getText().toString();
String City1input = City1.getText().toString();
String State1input=State1.getText().toString();
String Zip1input = Zip1.getText().toString();
String Address2input =Address2.getText().toString();
String City2input = City2.getText().toString();
String State2input = State2.getText().toString();
String Zip2input= zip2.getText().toString();
OutputStreamWriter out = new OutputStreamWriter(openFileOutput("myaddress.txt",0));
String[] mySettings ={NameofRouteinput,Address1input,City1input, State1input,Zip1input,Address2input,City2input,State2input,Zip2input,";"};
for(int i =0; i < mySettings.length; i++)
out.write(mySettings[i]);
out.close();
}
catch (java.io.IOException e){
}
Intent i = new Intent(AddOrModify.this, Frontpage.class);
startActivity(i);
}
}
private class btnlistenercancel implements View.OnClickListener{
public void onClick(View v) {
Intent i = new Intent(AddOrModify.this, Frontpage.class);
startActivity(i);
}
}
}