티스토리 뷰

반응형


import java.io.FileOutputStream;
import java.io.*;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class test {

 static int sum = 0;
 public static String getTodayDate(String flag){
    String todayDate = "";
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat dateFormat = null;
    if( "1".equals(flag) ){
     dateFormat = new SimpleDateFormat("yyyyMMdd");
    }else if("2".equals(flag)){
     dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    }
    todayDate = dateFormat.format(calendar.getTime());
    return todayDate;
 }

 
 public static void main(String args[])throws IOException{
  String log_content = "test" ;
  String file_header = "D:/";
  String file_tail = ".log";
  
  String file_name = getTodayDate("1");
  String file_path = file_header + file_name + file_tail ;
  FileOutputStream fos = null;
  OutputStreamWriter osw = null;
  try{
   fos = new FileOutputStream( file_path , true);
   osw = new OutputStreamWriter(fos);
   synchronized(osw){
    osw.write( "[" + getTodayDate("2") + "]" + "\t" + log_content+ "\r\n" );
    osw.flush();
   }
  } catch(Exception e){
   e.printStackTrace();
  } finally {
   if(osw != null){
    osw.close();
   }
   if(fos != null){
    fos.close();
   }
  }
  
 }
}

반응형
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함