字符串保存为txt文件,读取txt文件内容
最近在项目中为了测试从中间件MQ传递的数据是否正常,需要把接收到的xml格式的字符串保存下来进行分析,所以做个记录。
1、把字符串写入txt文件中
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date(System.currentTimeMillis());
String filename = format.format(date); //保存的文件名
String path = "D:\\mq\\" + filename + ".txt"; //保存的路径
textToFile(path,data);
//把字符串写入文件中
public void textToFile(final String strFilename, final String strBuffer){
try{
File fileText = new File(strFilename); // 创建文件对象
FileWriter fileWriter = new FileWriter(fileText); // 向文件写入对象写入信息
fileWriter.write(strBuffer); // 写文件
fileWriter.close();
}catch (IOException e){
e.printStackTrace();
}
}
2、读取txt文件的内容
String filepath = "F:\\mq