我正在创建一个简单的网络应用程序,并希望在启动时加载聊天记录,这可以正常工作,但它将文本格式化为一行
写入档案
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
答案 0 :(得分:0)
您应该可以使用Html.toHtml和Html.fromHtml保存格式。
有些事情:
{
"_index" : "logstash-2016.04.21",
"level1" : {
"level2" : {
"1" : "somevalue",
"2" : "somevalue2"
}
}
}
然后:
String htmlFormatted = Html.toHtml(tv.getText());
// .. save to file ..
请参阅以下类似问题: