Java 将两个字符串保存到一个文本文件而不中断彼此

Java 将两个字符串保存到一个文本文件而不中断彼此,java,android,string,file,save,Java,Android,String,File,Save,我想同时将两个字符串保存到一个文本文件中。其中一个字符串持续输入数据,而另一个字符串仅在单击按钮时写入数据。但是,当我单击按钮将其中的值添加到txt文件时,它会写入,但会删除前面其他字符串中的所有数据。之后,另一个字符串恢复正常写入 以下是打印到文件中的两个字符串的代码: public void onClick(View view) { try { EditText bodyText;

我想同时将两个字符串保存到一个文本文件中。其中一个字符串持续输入数据,而另一个字符串仅在单击按钮时写入数据。但是,当我单击按钮将其中的值添加到txt文件时,它会写入,但会删除前面其他字符串中的所有数据。之后,另一个字符串恢复正常写入

以下是打印到文件中的两个字符串的代码:

public void onClick(View view) {
                try {
                    EditText bodyText;
                    bodyText = (EditText) findViewById(R.id.ETName);
                    String name = bodyText.getText().toString();

                    PrintWriter out = new PrintWriter(new BufferedWriter(
                            new FileWriter("/sdcard/Accelerometer.html")));
                    out.println("<h3 style=padding-left:20px;>" + name
                            + "</h3><br>");
                    // new arraylist
                    out.close();
                } catch (IOException e) {
                    Log.e(TAG, "Could not write file " + e.getMessage());
                    e.printStackTrace();

                }
            }

        });

        try {
            PrintWriter writer = new PrintWriter(new BufferedWriter(
                    new FileWriter("/sdcard/Accelerometer.html", true)));
            writer.println("<h3 style=padding-left:20px;>" + text
                    + "</h3><br>");

            writer.close();
        } catch (IOException e) {

            e.printStackTrace();


        }

    }
public void onClick(视图){
试一试{
编辑文本正文;
bodyText=(EditText)findViewById(R.id.ETName);
字符串名称=bodyText.getText().toString();
PrintWriter out=新的PrintWriter(新的BufferedWriter(
新的FileWriter(“/sdcard/accelerator.html”);
out.println(““+名称
+“
”; //新阵列列表 out.close(); }捕获(IOE异常){ Log.e(标记“无法写入文件”+e.getMessage()); e、 printStackTrace(); } } }); 试一试{ PrintWriter writer=新的PrintWriter(新的BufferedWriter( 新的FileWriter(“/sdcard/accelerator.html”,true)); writer.println(“+text +“
”; writer.close(); }捕获(IOE异常){ e、 printStackTrace(); } }
任何帮助都会很好,谢谢。

修复了它:

Button confirmButton = (Button) findViewById(R.id.baddNametoFile);
        confirmButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                try {
                    EditText bodyText;
                    bodyText = (EditText) findViewById(R.id.ETName);
                    String name = bodyText.getText().toString();

                    PrintWriter out = new PrintWriter(new BufferedWriter(
                            new FileWriter("/sdcard/Accelerometer.html", true)));
                    out.println("<h3 style=padding-left:20px;>" + name
                            + "</h3><br>");

                    out.close();
                } catch (IOException e) {
                    Log.e(TAG, "Could not write file " + e.getMessage());
                    e.printStackTrace();

                }
            }

        });

        try {
            PrintWriter writer = new PrintWriter(new BufferedWriter(
                    new FileWriter("/sdcard/Accelerometer.html", true)));
            writer.println("<h3 style=padding-left:20px;>" + text
                    + "</h3><br>");
            // new arraylist
            writer.close();
        } catch (IOException e) {
            // put notification here later!!!
            e.printStackTrace();


        }

    }
按钮confirButton=(按钮)findViewById(R.id.baddNametoFile);
confirButton.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图){
试一试{
编辑文本正文;
bodyText=(EditText)findViewById(R.id.ETName);
字符串名称=bodyText.getText().toString();
PrintWriter out=新的PrintWriter(新的BufferedWriter(
新的FileWriter(“/sdcard/accelerator.html”,true));
out.println(““+名称
+“
”; out.close(); }捕获(IOE异常){ Log.e(标记“无法写入文件”+e.getMessage()); e、 printStackTrace(); } } }); 试一试{ PrintWriter writer=新的PrintWriter(新的BufferedWriter( 新的FileWriter(“/sdcard/accelerator.html”,true)); writer.println(“+text +“
”; //新阵列列表 writer.close(); }捕获(IOE异常){ //稍后把通知放在这里!!! e、 printStackTrace(); } }
基本上,我希望将两个字符串保存到一个文件中,然后在文件中共存(无覆盖等)。我的第一个代码示例放在一个字符串中,然后用另一个字符串重写。新代码现在将两个字符串写入相同的内容,因此两个字符串都不会相互覆盖