Optimiser vitesse de lectuere d'un fichier

AngelOfErian Messages postés 4 Date d'inscription mercredi 13 février 2008 Statut Membre Dernière intervention 8 juin 2008 - 8 juin 2008 à 11:14
Ombitious_Developper Messages postés 2333 Date d'inscription samedi 28 février 2004 Statut Membre Dernière intervention 26 juillet 2013 - 9 juin 2008 à 14:47
Boujour à tous,

Je suis en train de développer une application pour analyser des fichier de log. Ces fichiers on une taille assez importante (entre 4 et 10 Mo). Le temps de lecture moyen d'un de ces fichiers est de 3 minutes. Si en plus de les lire, j'effectue des traitement, celà peut prendre jusqu'a une heure.

J'aurai donc voulu savoir si quelqu'un avait une idée sur la marche à suivre pour réussir à gagner du temps. C'est très important, c'est pour mon stage de fin d'année.

public class LectureLog{

ArrayList<string> paths;
private ConnexionHSQL c;

public LectureLog(ConnexionHSQL c){
this.c = c;
}

public void readLogLines(String path) {
long start = System.currentTimeMillis();
boolean ok = true;
String s;
lireFichier(path);
FileReader enterprise;
Fichiers fic = new Fichiers();
try {
while(ok){
enterprise = new FileReader("tmp.txt");

BufferedReader ent_buf = new BufferedReader(enterprise);
//On parcours le fichier ligne par ligne.

if ((s = ent_buf.readLine()) != null) {
String str[] = s.split(" ");
String ip = str[3];
String date = str[0];
String heure = str[1];
String page = str[2];
// On récupère l'identifiant de la periode correspondant à la date de la ligne
ResultSet rs = c.executeQuery("SELECT numJ FROM JOUR WHERE date='"+date+"' AND heure='"+heure+"'");
// On regarde si la page visitée est une page à suivre
//ResultSet rs2 = c.executeQuery("SELECT * FROM PAGE_SUIV WHERE place='"+str[5].toLowerCase()+"'");
ResultSet rs7 = c.executeQuery("SELECT * FROM PAGE WHERE place='"+page+"'");
try {
if(!rs7.next()){
ResultSet rs8 = c.executeQuery("INSERT INTO PAGE (place) VALUES ('"+page+"')");
}
if(!rs.next()){
ResultSet rs1 = c.executeQuery("INSERT INTO JOUR (date, heure) VALUES ('"+date+"','"+heure+"')");
}
// Si la date est déjà dans la base de données
rs7 = c.executeQuery("SELECT * FROM PAGE WHERE place='"+page+"'");
if(rs7.next()){
// On verifie que pour la periode l'ip n'a pas consulter la page
ResultSet rs6 = c.executeQuery("SELECT * FROM CONSULTATION WHERE date='"+rs.getInt("numJ")+"' AND ip='"+ip+"' AND numP='"+rs7.getInt("numP")+"'");
// Si elle ne la pas fait on ajout les infos à la table CONSULTATION
if(!rs6.next()){
ResultSet rs1 = c.executeQuery("INSERT INTO CONSULTATION (date, ip, nbrIp, numP) VALUES ('"+rs.getString("numJ")+"','"+ip+"','"+compterIp(Integer.parseInt(heure), ip,rs7.getInt("numP"))+"','"+rs7.getInt("numP")+"')");
}
}
} catch (SQLException e) {
}
//
}
else{
ok = false;
}
ent_buf.close();
fic.supprimerLigne("tmp.txt");
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
File f = new File("tmp.txt");
f.delete();
long stop = System.currentTimeMillis();
System.out.println(" "+ (stop-start));
}

public int compterIp(int heure, String ip, int numP) throws NumberFormatException, IOException{
int i = 0;
String s;
FileReader enterprise;
enterprise = new FileReader("tmp.txt");
BufferedReader ent_buf = new BufferedReader(enterprise);
while ((s = ent_buf.readLine()) != null) {
String[] str = s.split(" ");
ResultSet rs = c.executeQuery("SELECT numP FROM PAGE WHERE place='"+str[2]+"'");
try {
if(rs.next()){
if(str[3].equalsIgnoreCase(ip) && Integer.parseInt(str[1]) == heure && rs.getInt("numP") == numP){
i++;
}
}
} catch (SQLException e) {
Erreur err = new Erreur(e.getMessage(),"JAnaLog::Erreur");
}
}
ent_buf.close();
return i;
}

public void lireFichier (String path){
FileWriter fw;
try {
fw = new FileWriter("tmp.txt");
String s;
FileReader enterprise;
enterprise = new FileReader(path);
BufferedReader ent_buf = new BufferedReader(enterprise);
while ((s = ent_buf.readLine()) != null) {
Pattern p = Pattern.compile("^#");
Matcher m = p.matcher(s);
if(!m.find()){
s = s.replace("'", "_");
String str[] = s.split(" ");
String strh[] = str[1].split(":");
Pattern pp = Pattern.compile("gif$|png$|jpg$|white$|/$",Pattern.CASE_INSENSITIVE);
Matcher mm = pp.matcher(str[5]);
if(!mm.find()){
String l = str[0]+" "+strh[0]+" "+str[5].toLowerCase()+" "+str[9];
fw.write(l);
fw.write('\n');
}
}
}
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}</string>

7 réponses

cs_jojolemariole Messages postés 519 Date d'inscription mercredi 21 mars 2007 Statut Membre Dernière intervention 19 décembre 2016 25
8 juin 2008 à 11:17
salut,

je ne lirai pas ce code lol remets-le en forme s'il te plait
0
AngelOfErian Messages postés 4 Date d'inscription mercredi 13 février 2008 Statut Membre Dernière intervention 8 juin 2008
8 juin 2008 à 12:55
On ne peut pas éditer ses messages sur ce forum?
0
cs_Chatbour Messages postés 764 Date d'inscription jeudi 27 juillet 2006 Statut Membre Dernière intervention 6 septembre 2010 19
8 juin 2008 à 13:36
non..
0
AngelOfErian Messages postés 4 Date d'inscription mercredi 13 février 2008 Statut Membre Dernière intervention 8 juin 2008
8 juin 2008 à 14:36
public void lireFichier (String path){
FileWriter fw;
try {
fw = new FileWriter("tmp.txt");
String s;
FileReader enterprise;
enterprise = new FileReader(path);
BufferedReader ent_buf = new BufferedReader(enterprise);
while ((s = ent_buf.readLine()) != null) {
Pattern p = Pattern.compile("^#");
Matcher m = p.matcher(s);
if(!m.find()){
s = s.replace("'", "_");
String str[] = s.split(" ");
String strh[] = str[1].split(":");
Pattern pp = Pattern.compile("gif$|png$|jpg$|white$|/$",Pattern.CASE_INSENSITIVE);
Matcher mm = pp.matcher(str[5]);
if(!mm.find()){
String l = str[0]+" "+strh[0]+" "+str[5].toLowerCase()+" "+str[9];
fw.write(l);
fw.write('\n');
}
}
}
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
AngelOfErian Messages postés 4 Date d'inscription mercredi 13 février 2008 Statut Membre Dernière intervention 8 juin 2008
8 juin 2008 à 14:40
Bon ben tant pis pour la mise en page...

Globalement ça marche de la façon suivante: j'ai un FileReader et un BufferedReader, et ensuite je fait un readLine tant que j'ai pas attend la fin de mon fichier.

Quelqu'un a t il une meilleurs méthodes pour lire un fichier?
0
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 111
9 juin 2008 à 10:07
Salut,

pour ceux qui veulent :

la mise en page du code correspondant au message 08/06/2008 14:36:38)
public void lireFichier(final String path) {
        FileWriter fw;
        try {
            fw = new FileWriter("tmp.txt");
            String s;
            FileReader enterprise;
            enterprise = new FileReader(path);
            BufferedReader ent_buf = new BufferedReader(enterprise);
            while ((s = ent_buf.readLine()) != null) {
                Pattern p = Pattern.compile("^#");
                Matcher m = p.matcher(s);
                if (!m.find()) {
                    s = s.replace("'", "_");
                    final String str[] = s.split(" ");
                    final String strh[] = str[1].split(":");
                    Pattern pp = Pattern.compile("gif$|png$|jpg$|white$|/$",
                            Pattern.CASE_INSENSITIVE);
                    Matcher mm = pp.matcher(str[5]);
                    if (!mm.find()) {
                        final String l = str[0] + " " + strh[0] + " "
                                + str[5].toLowerCase() + " " + str[9];
                        fw.write(l);
                        fw.write('\n');
                    }
                }
            }
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

------------------------------------
"On n'est pas au resto : ici on ne fait pas dans les plats tout cuits ..."

OoWORAoO
0
Ombitious_Developper Messages postés 2333 Date d'inscription samedi 28 février 2004 Statut Membre Dernière intervention 26 juillet 2013 38
9 juin 2008 à 14:47
Salut:

Dommage de les éditeurs actuels ne disposent pas de balises HTML pour formater le code source comme dans la majorité des sites de code sources. (
 ... 
)
0
Rejoignez-nous