Попробуйте это
public static boolean writeLog(String user , Exception exS) {
boolean d = false;
try {
File root = new File("TVS_log");
if (!root.exists()) {
root.mkdirs();
}
String name = user + "_" + TimeReporter.getTodayAll() + "_" + "log.txt" ;
System.out.println(name);
File text = new File(root , name);
if (!text.exists()) {
text.createNewFile();
}
String aggregate = "";
for (StackTraceElement element : exS.getStackTrace())
{
BufferedWriter writer = new BufferedWriter(new FileWriter(text)) ;
String message = exS.toString() + " - " + element.toString() + "\r\n" ;
System.out.println(exS.toString());
System.out.println(element.toString());
aggregate += message;
writer.write (aggregate);
writer.newLine();
writer.flush();
writer.close();
writer = null;
}
if(text.exists())
return text.length() > 0;
}catch(Exception ex){
ex.printStackTrace();
}
return d;
}
Класс и функция TimeReporter
public class TimeReporter {
public static String getTodaysDate() {
String d = "";
final Calendar c = Calendar.getInstance();
d = String.format("%02d", c.get(Calendar.YEAR))
+ String.format("%02d", c.get(Calendar.MONTH) + 1)
+ String.format("%02d", c.get(Calendar.DAY_OF_MONTH));
return d;
}
public static String getTodaysTime() {
String d = "";
final Calendar c = Calendar.getInstance();
d = String.format("%02d", c.get(Calendar.HOUR_OF_DAY))
+ String.format("%02d", c.get(Calendar.MINUTE))
+ String.format("%02d", c.get(Calendar.SECOND));
return d;
}
public static String getTodayAll() {
return getTodaysDate() + "_" + getTodaysTime() ;
}
public static String getNow() {
final Calendar c = Calendar.getInstance();
String ld = c.get(Calendar.YEAR) + "/" + (c.get(Calendar.MONTH) + 1)
+ "/" + c.get(Calendar.DAY_OF_MONTH) + " "
+ String.format("%02d", c.get(Calendar.HOUR_OF_DAY)) + ":"
+ String.format("%02d", c.get(Calendar.MINUTE));
return ld;
}
}