package com.aiatss.coast.common.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestLog {
public static void main(String[] args) {
for(int i=0;i<2;i++){
test("898989");
}
}
public static void test(String str){
String path01=( new SimpleDateFormat("HH时mm分ss秒") ).format(new Date(System.currentTimeMillis()));
String path02=( new SimpleDateFormat("HH时mm分") ).format(new Date(System.currentTimeMillis()));
String path03=( new SimpleDateFormat("HH时") ).format(new Date(System.currentTimeMillis()));
String path04=( new Date() ).getTime() +"";
String logPath="d:\\temp_log\\temp log "+path02+".txt";
// 编辑要写入的内容
String temp="";
if(str==null) temp="str is null";
else temp=str;
FileWriter writer;
try {
// 如果没有文件夹,创建文件夹
File folder = new File("d:\\temp_log");
if(!folder.exists()){
folder.mkdirs();
}
// 创建txt文件,如果没有,自动创建,如果有,不覆盖
File file = new File(logPath);
if(file.isFile() && file.exists()){
//System.out.println("文件存在");
}else{
file.createNewFile();
}
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(logPath, true), "UTF-8"));
bufw.write(path01);
bufw.newLine();
bufw.write(temp);
bufw.newLine();
bufw.flush();
bufw.close();
} catch (Exception e) {
}finally{
System.err.println(path01);
readTxtFile(logPath);
}
}
public static void readTxtFile(String filePath){
try {
String encoding="UTF-8";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
}
联系客服