java FileOutputStream 특정폴더 떨구기
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();
}
}
}
}