最近上手QQ小程序,想展示文章内容,无奈自己没有服务器、数据库,QQ小程序又没有云函数,怎么办呢?
只能把文章内容存放在小程序页面data中了,但是一篇文章几十行、甚至上百行,有的还有图片。
于是乎找了个富文本编辑器,把文本内容输入、格式调好在转换成HTML内容。看下图。
然后再转成HTML内容。
但是直接复制过去吧,看下图,得一行行的缩进,比较麻烦。
索性就写一段java代码把多行文本转换成一行文本,话不多说,看代码。
public static void main(String[] args) {String path ="G:one.txt";//文件路径//读取多行文本转换为一行文本String content = InTextOut.readFileContent(path);//再把一行文本输出到文本中String finallyPath = "G:finall.txt";InTextOut.wirteContentFile(finallyPath, content);}public static void wirteContentFile(String path,String content){BufferedWriter buffWriter = null;try {FileWriter fileWriter = new FileWriter(path);buffWriter =new BufferedWriter(fileWriter); buffWriter.write(content);} catch (Exception e) {e.printStackTrace();}finally{if(buffWriter!=null){try {buffWriter.close();} catch (IOException e) {e.printStackTrace();}}}}public static String readFileContent(String fileName) { File file = new File(fileName); BufferedReader reader = null; StringBuffer sbf = new StringBuffer(); try { reader = new BufferedReader(new FileReader(file)); String tempStr; while ((tempStr = reader.readLine()) != null) { sbf.append(tempStr); } reader.close(); return sbf.toString(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { e1.printStackTrace(); } } } return sbf.toString();}
多行文本转换前后比较。
现在复制单行文本到data中就简单多了。
看看用towxml插件把html转换成wxml渲染的效果。
本文系作者原创,转载请注明出处。