java FileOutputStream 특정폴더 떨구기

Programing 2010.04.12 20:13 Posted by 애플자라


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();
   }
  }
  
 }
}

댓글을 달아 주세요

  1. Favicon of https://dsdstudio.tistory.com BlogIcon DSD군 2010.05.25 00:19 신고  댓글주소  수정/삭제  댓글쓰기

    파일처리관련 스트림이나 writer 닫을때
    commons-io 에서 IOUtils.closeQuietly(fis); 요것도 단순하고 괜찮엉 ㅋ