【java】指定文件夹下创建日志文件并写入

private static final String DEVICE_LOG_PATH = "D:\\google_download\\device_log"
// 主方法
private void syncLogToPath(JSONObject propertiesJsonObject) {
String deviceIotId = propertiesJsonObject.getString(DEVICE_ID_COLUMN);
String status = propertiesJsonObject.getString(MESSAGE_TYPE);
int currentIotState = Constants.hcDeviceStatus.get(status);
String currentDay = DateUtil.format(new Date(), "yyyyMMdd");
String newFilePath = DEVICE_LOG_PATH + "\\" + currentDay + ".txt";
File file = new File(DEVICE_LOG_PATH);
// 判断文件夹是否存在,不存在则创建
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("kingid");
bw.flush();
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileReader fr = new FileReader(file);
BufferedReader bReader = new BufferedReader(fr);
String string = bReader.readLine();
System.out.println(string);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
上一篇:Java中的转换流


下一篇:jinal 反射取值问题