最近上手QQ小程序,想展現文章內容,無奈本身沒有服務器、數據庫,QQ小程序又沒有云函數,怎麼辦呢?
只能把文章內容存放在小程序頁面data中了,可是一篇文章幾十行、甚至上百行,有的還有圖片。
因而乎找了個富文本編輯器,把文本內容輸入、格式調好在轉換成HTML內容。看下圖。
而後再轉成HTML內容。
可是直接複製過去吧,看下圖,得一行行的縮進,比較麻煩。
索性就寫一段java代碼把多行文本轉換成一行文本,話很少少,看代碼。html
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渲染的效果。
已經實現QQ小程序
java