gpt4 book ai didi

android - 将字符串保存为 html 文件 android

转载 作者:太空宇宙 更新时间:2023-11-03 12:50:55 26 4
gpt4 key购买 nike

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

假设我的字符串是:

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

最佳答案

试试这个。

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

关于android - 将字符串保存为 html 文件 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31553402/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com