La copie de code est la suivante:
package com.teligen.eos.telecode;
Importer java.io.file;
import java.io.filewriter;
Importer java.io.ioException;
Importer java.util.date;
/ **
* Écrivez des informations sur le journal dans le fichier spécifié
* /
classe publique WriteLogutil {
String statique privé rootpath = "d: // logs //";
/ **
* Écrivez des informations dans le fichier
* @param msg
* /
public static void writememsgtofile (String msg) {
// Supprimer les fichiers précédents
deloldfile ();
FileWriter FileWriter = NULL;
essayer {
FileWriter = new FileWriter (getFileName (), true);
Date aujourd'hui = 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 ("#### Écrire journal dans l'exception de fichier #### >>>" + e.getMessage ());
e.printStackTrace ();
} enfin {
essayer {
filewriter.close ();
} catch (ioexception e) {
System.out.println ("### Fermez l'exception du flux pour la rédaction de journaux ### >>" + e.getMessage ());
e.printStackTrace ();
}
}
}
/ **
* Supprimer les fichiers journaux précédents
* /
vide statique privé deloldfile () {
Date aujourd'hui = new Date ();
int mois = aujourd'hui.getMonth () + 1;
mois = mois - 2;
si (mois == -1) mois = 11;
si (mois == 0) mois = 12;
String delpath = rootpath + string.valueof (mois) + "//";
Fichier de fichiers = nouveau fichier (DelPath);
if (dosder.exists ()) {
File [] files = dosder.listFiles ();
for (int i = 0; i <files.length; i ++) {
fichiers [i] .delete ();
}
}
}
/ **
* Faites enregistrer le fichier
* @return nom de fichier
* /
chaîne statique privée getFileName () {
Date aujourd'hui = new Date ();
String FileName = String.ValueOf ((Today.GetyEar () + 1900)) + String.ValueOf ((Today.getMonth () + 1)) + String.Valueof (Today.getDate ()) + ".log";
// Créer un répertoire
Fichier de fichiers = nouveau fichier (rootpath + string.valueof ((aujourd'hui.getMonth () + 1)) + "//");
if (! Folder.exists ()) {
dossier.mkDirs ();
}
// Créer un fichier
Fichier fichier = nouveau fichier (nom de fichier);
if (! file.exists ()) {
essayer {
file.CreateEwFile ();
} catch (ioexception e) {
System.out.println ("#### Exception du fichier journal #### >>>" + e.getMessage ());
e.printStackTrace ();
}
}
filename = rootpath + string.valueof ((aujourd'hui.getMonth () + 1)) + "//" + nom de fichier;
Nom de fichier de retour;
}
/ **
* La méthode principale utilisée pour tester
* /
public static void main (String [] args) {
// getFileName ();
String testString = "Write logs: 71FABB7890D2CC0D267FBD84F409618C0303BC597B9244C324947BDE4B1C0B4CB08C33FC461F7BADD088535DAE42D8D7D06F4134E442D9D1CE3A0F9B3EDD64337A2D18CE34FCDC137B7CBD84 F409618C03038FEAEC79F79C2F58BD84F409618C03038FEAEC79F79C2F58BD84F40 2EDE5AC3C8FAF2FD8A686E ";
WriteMsGtoFile (testString);
// deloldfile ();
}
}