【发布时间】: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