Android:附加字符串不起作用

时间:2011-05-20 15:31:22

标签: java android string append

有人可以告诉我下面的代码有什么问题吗? 我是java和android dev的新手,非常感谢先进! :)

public class GetContentThenAppend extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView tv2;
        tv2 = (TextView) this.findViewById(R.id.thetext);
        tv2.setText("This is the writing program...\n");

        try{
                /*
               File f = new File(Environment.getExternalStorageDirectory()+"/EngagiaDroid/videos.html");
               FileInputStream fileIS = new FileInputStream(f);
               BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
               String readString = new String();

               //just reading each line and pass it on the debugger
               int x = 1;
               while((readString = buf.readLine())!= null){
                   tv2.append( Integer.toString(x) + ":> " );
                   tv2.append( readString );
                   tv2.append( "\n" );
                   x++;
               }
                */

            final String entryString = new String("" +
                    "<div stle='color: red; border: this solid red;'>" +
                    "Hey yo this is the new content!!!" +
                    "</div>" +
                    "</div>" +
                    "</div>" +
                    "</body>" +
                    "</html>");

            String htmlFile = Environment.getExternalStorageDirectory()+"/EngagiaDroid/videos.html";
            FileOutputStream fOut = openFileOutput(htmlFile, MODE_APPEND);
            OutputStreamWriter osw = new OutputStreamWriter(fOut);  

            osw.write(entryString);

            osw.flush();
            osw.close();


            } catch (FileNotFoundException e) {
               e.printStackTrace();
            } catch (IOException e){
               e.printStackTrace();
            }
    }
}

我正在尝试通过向其添加一些字符串/ html代码来更新html内容。当我试图运行它时,我收到一条强制关闭消息。

1 个答案:

答案 0 :(得分:0)

我最终使用了另一种方法。我使用了ff代码:

    String TESTSTRING = "<div style='color: blue; border: thin solid red;'>YO!</div>";

    File root = Environment.getExternalStorageDirectory();
    if (root.canWrite()){
        File gpxfile = new File(root, "samplefile.html");
        FileWriter gpxwriter = new FileWriter(gpxfile, true);
        BufferedWriter out = new BufferedWriter(gpxwriter);
        out.write(TESTSTRING);
        out.close();
    }