14

我有一个需要读取和写入文本文件的应用程序。我有它读,但我没有它写。这个想法是,当我单击屏幕上的保存按钮时,它会将已放入 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);
            }

        }

}
4

2 回答 2

17

有了 mari 的解决方案,我得到了

       java.lang.IllegalArgumentException: contains a path separator

然后我尝试了以下,它对我有用

  File root = new File(DIRECTORY_PATH);
  File gpxfile = new File(root, "samples.txt");
  FileWriter writer = new FileWriter(gpxfile);
  writer.append("First string is here to be written.");
  writer.flush();
  writer.close();

你可以循环它来写多行

于 2015-01-07T09:19:13.790 回答
6

您可以使用FileOutputStream代替OutputStreamWriter,如下所示:

File file = getFileStreamPath("test.txt");

if (!file.exists()) {
   file.createNewFile();
}

FileOutputStream writer = openFileOutput(file.getName(), Context.MODE_PRIVATE);

for (String string: data){
    writer.write(string.getBytes());
    writer.flush();
}

writer.close();

检查安卓文档

于 2012-12-08T16:29:22.607 回答