保存String作为HTML文件的android [英] Save String as html file android

查看:448
本文介绍了保存String作为HTML文件的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题仅仅是,我怎么字符串的HTML保存在内部存储.html文件。请让我知道如何做到这一点。

可以说字符串我是:

 字符串的html =< HTML>< HEAD><标题>标题< /标题>< /头><车身>这是随机的文字< /身体GT;< / HTML>中


解决方案

试用此。

 私人无效saveHtmlFile(){            字符串路径= Environment.getExternalStorageDirectory()的getPath()。
            字符串文件名= DateFormat.format(dd_MM_yyyy_hh_mm_ss,System.currentTimeMillis的())的toString()。
            文件名=文件名+。html的;
            档案文件=新的文件(路径,文件名);
            字符串的html =< HTML>< HEAD><标题>标题< /标题>< /头><车身>这是随机的文字< / BODY>< / HTML>中;            尝试{
                FileOutputStream中出=新的FileOutputStream(文件);
                字节[]数据= html.getBytes();
                out.write(数据);
                out.close();
                Log.e(TAG,文件保存:+ file.getPath());
            }赶上(FileNotFoundException异常五){
                e.printStackTrace();
            }赶上(IOException异常五){
                e.printStackTrace();
            }
        }

My question simply is that how do I save string HTML as .html file in internal storage. Please let me know how to do this.

Lets say string I have is:

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

解决方案

Tryout This.

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

这篇关于保存String作为HTML文件的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆