【问题标题】:Reading text file keeping previous format读取保持先前格式的文本文件
【发布时间】:2016-04-21 20:23:21
【问题描述】:

我正在制作一个简单的网络应用程序并希望在启动时加载聊天日志,这工作正常,但它将文本格式化为一行

写入文件

CharSequence cs = tv.getText ();
final String str = cs + "\r\n" + s;
//Write to text file
try {
FileOutputStream fos = openFileOutput("Chat Log", Context.MODE_APPEND);
fos.write(s.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}



}
catch (IOException e1)
{

e1.printStackTrace();
}
//close the socket
socket.close(); 

阅读文本

final TextView tv = (TextView)findViewById(R.id.textView1);
try {
BufferedReader inputReader = new BufferedReader(new InputStreamReader(
openFileInput("Chat Log")));
String inputString;
StringBuffer stringBuffer = new StringBuffer();
while ((inputString = inputReader.readLine()) != null) {
stringBuffer.append(inputString + "\r\n");
tv.append(inputString);
}

} catch (IOException e) {
e.printStackTrace();
}

当前结果 Screenshot

【问题讨论】:

    标签: android android-studio fileinputstream fileoutputstream


    【解决方案1】:

    您应该能够使用Html.toHtmlHtml.fromHtml 保存格式。

    类似的东西:

    String htmlFormatted = Html.toHtml(tv.getText());
    // .. save to file ..
    

    然后:

    Spanned htmlFormatted = Html.fromHtml(chatLog);
    tv.setText(htmlFormatted);
    

    查看以下类似问题:

    【讨论】:

    • @josh-reilly 请接受答案,如果没有帮助请告诉我。
    猜你喜欢
    • 2013-02-09
    • 2017-04-26
    • 2016-07-28
    • 2018-10-16
    • 2012-11-25
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多