我怎样写在Android的.txt文件?写在、文件、Android、txt

2023-09-06 16:36:15 作者:自己能抗就别声张

我有一个应用程序需要读取和写入到一个文本文件中。我把它看完,但我不把它写。我们的想法是,当我在屏幕上点击保存按钮,它会保存所有已投入textviews到一个数组中,并写入阵列的每个段到文本文件中的信息这是我的code为写入部分

 公共类AddOrModify延​​伸活动{

    私人按钮Savebtn;
    私人按钮Cancelbtn;
    私人的EditText NameofRoute;
    私人的EditText地址1;
    私人的EditText City1;
    私人的EditText状态1;
    私人的EditText ZIP1;
    私人的EditText地址2;
    私人的EditText城2;
    私人的EditText状态2;
    私人的EditText拉链码;




        @覆盖
        公共无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.add_or_modify);

            Savebtn =(按钮)findViewById(R.id.savebtn);
            Savebtn.setOnClickListener(新btnlistenersave());

            Cancelbtn =(按钮)findViewById(R.id.cancelbtn);
            Cancelbtn.setOnClickListener(新btnlistenercancel());

        }

        私有类btnlistenersave实现View.OnClickListener {
            公共无效的onClick(视图v){

                NameofRoute =(EditText上)findViewById(R.id.NameofRoute);
                地址1 =(EditText上)findViewById(R.id.editAddress1);
                City1 =(EditText上)findViewById(R.id.C​​ity1);
                状态1 =(EditText上)findViewById(R.id.State1);
                ZIP1 =(EditText上)findViewById(R.id.Zip1);
                地址2 =(EditText上)findViewById(R.id.Address2);
                城2 =(EditText上)findViewById(R.id.C​​ity2);
                状态2 =(EditText上)findViewById(R.id.State2);
                拉链码=(EditText上)findViewById(R.id.Zip2);

                //的String [] mySettings = {NameofRouteinput,Address1input,City1input,State1input,Zip1input,Address2input,City2input,State2input,Zip2input,};


               //如果(mySettings!= NULL){
                尝试{

                    字符串NameofRouteinput = NameofRoute.getText()的toString()。
                    字符串Address1input = Address1.getText()的toString()。
                    字符串City1input = City1.getText()的toString()。
                    字符串State1input = State1.getText()的toString()。
                    字符串Zip1input = Zip1.getText()的toString()。
                    字符串Address2input = Address2.getText()的toString()。
                    字符串City2input = City2.getText()的toString()。
                    字符串State2input = State2.getText()的toString()。
                    字符串Zip2input = zip2.getText()的toString()。
                    OutputStreamWriter出=新OutputStreamWriter(openFileOutput(myaddress.txt,0));

                    的String [] mySettings = {NameofRouteinput,Address1input,City1input,State1input,Zip1input,Address2input,City2input,State2input,Zip2input,};


                    的for(int i = 0; I< mySettings.length;我++)
                    out.write(mySettings [I]);
                    out.close();
                }
                赶上(java.io.IOException异常E){

                }


                意图I =新的意图(AddOrModify.this,Frontpage.class);
                startActivity(ⅰ);

            }

        }

        私有类btnlistenercancel实现View.OnClickListener {

            公共无效的onClick(视图v){
                意图I =新的意图(AddOrModify.this,Frontpage.class);
                startActivity(ⅰ);
            }

        }

}
 

解决方案

您可以使用的FileOutputStream 而不是 OutputStreamWriter ,是这样的:

 文件fil​​e = getFileStreamPath(的test.txt);

如果(!file.exists()){
   file.createNewFile();
}

的FileOutputStream作家= openFileOutput(file.getName(),Context.MODE_PRIVATE);

对于(字符串字符串:数据){
    writer.write(string.getBytes());
    writer.flush();
}

writer.close();
 
金立f100l怎么解锁

检查机器人文档。

I have an app that needs to read and write to a text file. I have it reading, but I dont have it writing. The idea is that when i click the save button on the screen, its going to save all the info that has been put into the textviews into an array and the write each segment of the array into the text file This is my code for the writing portion.

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);
            }

        }

}

解决方案

You can use FileOutputStream instead of OutputStreamWriter, something like this:

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

Check the android docs.

 
精彩推荐