如何在Android中写入.txt文件?

时间:2012-12-08 16:13:59

标签: android textview outputstream

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

        }

}

2 个答案:

答案 0 :(得分:15)

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

你可以循环它来写多行

答案 1 :(得分: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();

检查android docs