0

I want to save two strings to one text file at the same time. One of the strings constantly inputs data, while the other only writes data when a button is clicked. However, when I click the button to add the value from there to the txt file, it writes, but it deletes all data from the other string before it. After, the other string goes back to writing as normal.

Here is the code for the two strings printing to the file:

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


        }

    }

Any help would be great, thanks.

4

1 回答 1

0

解决它:

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


        }

    }

基本上,我希望将两个字符串保存到一个文件中,然后在文件中共存(没有覆盖等)。我的第一个代码示例放在一个字符串中,然后用另一个字符串覆盖它。新代码现在将两个字符串写入相同的内容,因此不会相互覆盖。

于 2013-11-11T12:59:34.937 回答