A cópia do código é a seguinte:
pacote com.teligen.eos.telecode;
importar java.io.file;
importar java.io.filewriter;
importar java.io.ioException;
importar java.util.date;
/**
* Escreva informações de log no arquivo especificado
*/
classe pública WriteLogutil {
String estática privada rootpath = "d: // logs //";
/**
* Escreva informações para o arquivo
* @param msg
*/
public static void writemsgtofile (string msg) {
// Exclua arquivos anteriores
deloldfile ();
FileWriter fileWriter = null;
tentar {
fileWriter = new FileWriter (getFileName (), true);
Data hoje = new date ();
String time = string.valueof (Today.gethours ()) + ":" + string.valueof (Today.getminutes ()) + "" + string.valueof (Today.getSeconds ());
fileWriter.Write ("#" + time + "# [" + msg + "]" + "/r/n");
fileWriter.flush ();
} catch (ioexception e) {
System.out.println ("#### Escreva log no arquivo Excection #### >>>" + e.getMessage ());
E.PrintStackTrace ();
} finalmente {
tentar {
fileWriter.close ();
} catch (ioexception e) {
System.out.println ("### Feche a exceção do fluxo para escrever logs ### >>" + e.getMessage ());
E.PrintStackTrace ();
}
}
}
/**
* Exclua arquivos de log anteriores
*/
private static void deloldfile () {
Data hoje = new date ();
int mês = Today.getMonth ()+1;
mês = mês - 2;
if (mês == -1) mês = 11;
if (mês == 0) mês = 12;
String delpath = rootpath + string.valueof (mês) + "//";
Pasta de arquivo = novo arquivo (delpath);
if (pasta.exists ()) {
Arquivo [] arquivos = pasta.listfiles ();
for (int i = 0; i <files.length; i ++) {
arquivos [i] .Delete ();
}
}
}
/**
* Faça com que o arquivo seja salvo
* @return filename
*/
String estática privada getFileName () {
Data hoje = new date ();
String filename = string.valueof ((Today.getyear () + 1900)) + string.valueof ((Today.getMonth () + 1)) + string.valueof (Today.getDate ()) + ".Log";
// Crie um diretório
Pasta de arquivo = novo arquivo (rootpath + string.valueof ((Today.getMONTH () + 1)) + "//");
if (! pasta.exists ()) {
pasta.mkdirs ();
}
// Crie um arquivo
Arquivo de arquivo = novo arquivo (nome do arquivo);
if (! file.exists ()) {
tentar {
file.createNewFile ();
} catch (ioexception e) {
System.out.println ("#### Novo arquivo de arquivo de log #### >>>" + e.getMessage ());
E.PrintStackTrace ();
}
}
arquivo de arquivo = rootpath + string.valueof ((Today.getMonth () + 1)) + "//" + nome do arquivo;
nome do arquivo de retorno;
}
/**
* O principal método usado para testar
*/
public static void main (string [] args) {
// getFileName ();
String testString = "Write logs: 71FABB7890D2CC0D267FBD84F409618C0303BC597B9244C324947BDE4B1C0B4CB08C33FC461F7BADD088535DAE42D8D7D06F4134E442D9D1CE3A0F9B3EDD64337A2D18CE34FCDC137B7CBD84 F409618C03038FEAEC79F79C2F58BD84F409618C03038FEAEC79F79C2F58BD84F409618C03038FEAEC79F79C2F581790ACB3C178641D14D8C09905BC52CF1C8249B12F2EDE5AC3C8FAF2FD8A686E";
writemsgtofile (testString);
// deloldfile ();
}
}