将String保存为html文件android

时间:2015-07-22 03:36:13

标签: android html

我的问题只是如何将字符串HTML保存为内部存储中的.html文件。请让我知道如何做到这一点。

让我说我的字符串是:

string html="<html><head><title>Title</title></head><body>This is random text.</body></html>"

3 个答案:

答案 0 :(得分:5)

尝试这个。

private void saveHtmlFile() {

            String path = Environment.getExternalStorageDirectory().getPath();
            String fileName = DateFormat.format("dd_MM_yyyy_hh_mm_ss", System.currentTimeMillis()).toString();
            fileName = fileName + ".html";
            File file = new File(path, fileName);
            String html = "<html><head><title>Title</title></head><body>This is random text.</body></html>";

            try {
                FileOutputStream out = new FileOutputStream(file);
                byte[] data = html.getBytes();
                out.write(data);
                out.close();
                Log.e(TAG, "File Save : " + file.getPath());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

答案 1 :(得分:1)

[Products(id=1, product_name=product_1, qty=13, price=66.00, DEVICE_ID=1, created_at=2019-11-07 11:29:25, updated_at=2019-11-07 16:24:20), Products(id=2, product_name=product_2, qty=12, price=66.00, DEVICE_ID=1, created_at=2019-11-07 11:30:02, updated_at=null)]

答案 2 :(得分:0)

试试这个:

 public void writeHTML() throws IOException
    {
            String html="<html><head><title>Title</title></head><body>This is random text.</body></html>"
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("myfile.html", Context.MODE_PRIVATE));
            outputStreamWriter.write(html);
            outputStreamWriter.close();
    }