java将数据写入.txt文件(简单的IO操作)
2019-11-04 本文已影响0人
永不熄灭的火焰_e306
<u>啥话不说直接上demo:</u>
将数据写入txt文件
文件写入操作
File directory = new File("E:/outputData/");
directory.mkdir();//创建文件夹
FileOutputStream outputStream = null;
try {
File file = new File("E:/outputData/userData.txt");
if(!file.exists()) {
file.createNewFile();
}
outputStream = new FileOutputStream(file,true);//形参里面可追加true参数,表示在原有文件末尾追加信息
String data ="userId: "+ userId+"\t"+"username: "+ username+"\t"+"questionId: "+questionId+"\t"+"进入答题页面时间: "+loginInTime+"\t"
+"离开答题页面时间: "+leaveTime+"\t"+"页面停留时间: "+timeIntervalDate+"\r\n";
outputStream.write(data.getBytes("utf-8"));
result.setStatus(ResultStatus.SUCCESS.getCode());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}