1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class FilesFinder {
String source = null;
String destination = null;
String sujet = null;
String texte = "";
String date_envoie_message = null;
String bodyhtml = "";
public static Connection conn;
public static Statement stat;
public FilesFinder() throws ClassNotFoundException, SQLException {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:synchro_outlook_android.s3db");
stat = conn.createStatement();
stat.executeUpdate("drop table if exists fichier;");
stat.executeUpdate("create table fichier (date_envoie,source,destination,sujet,texte,bodyhtml,nom_dossier);");
stat.executeUpdate("drop table if exists attachement;");
stat.executeUpdate("create table attachement (nomattac,date_envoie);");
stat.executeUpdate("drop table if exists image;");
stat.executeUpdate("create table image (nomimage,date_envoie);");
}
public void insertFin(String path) throws IOException
{
FileWriter writer = null;
String texte = "FIN DU FICHIER";
try{
writer = new FileWriter(path, true);
writer.write(texte,0,texte.length());
}catch(IOException ex){
ex.printStackTrace();
}finally{
if(writer != null){
writer.close();
}
}
}
// procédure qui ouvre un dossier
public void findFiles(String directoryPath) throws SQLException, IOException {
int nbligne = 1;
String ligne = "";
File directory = new File(directoryPath);
File[] subfiles = directory.listFiles();
for(int i=0 ; i<subfiles.length; i++)
{
System.out.println(subfiles[i].getName());
System.out.println("-------------------------------------------");
String nom_fich = subfiles[i].getName();
String path = directoryPath+'/'+nom_fich;
insertFin(path);
HaveBody hb = new HaveBody(path);
HaveText hv = new HaveText(path);
HaveImage hi = new HaveImage(path);
HaveAttachement ha = new HaveAttachement(path);
try{
FileInputStream fis = new FileInputStream(path);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
while (nbligne <8)
{
ligne = reader.readLine();
if(nbligne == 1)
source = ligne;
else if(nbligne == 3)
destination = ligne;
else if(nbligne == 5)
sujet = ligne;
else if(nbligne == 7)
date_envoie_message = ligne;
nbligne++;
}
reader.close();
texte = hv.extractText();
bodyhtml = hb.extractBody();
hi.extractImage();
ha.extractAttachement();
}
catch (Exception e){
System.out.println(e.toString());
}
PreparedStatement prep = conn.prepareStatement("insert into fichier values (?,?,?,?,?,?,?);");
prep.setString(1,date_envoie_message );
prep.setString(2, source);
prep.setString(3, destination);
prep.setString(4, sujet);
prep.setString(5, texte);
prep.setString(6, bodyhtml);
prep.setString(7, directoryPath);
prep.addBatch();
conn.setAutoCommit(false);
prep.executeBatch();
conn.setAutoCommit(true);
for(int j = 0; j < hi.nbimage; j++)
{
/*PreparedStatement prep1 = conn.prepareStatement("insert into image values (?,?);");
prep1.setString(1,date_envoie_message );
prep1.setString(2, hi.nomimage[j]);
*/System.out.println(hi.nomimage[j]);
/*prep1.addBatch();
conn.setAutoCommit(false);
prep1.executeBatch();
conn.setAutoCommit(true);*/
}
for(int k = 0; k < ha.nbattach; k++)
{
// PreparedStatement prep2 = conn.prepareStatement("insert into attachement values (?,?);");
// prep2.setString(1,date_envoie_message );
// prep2.setString(2, ha.nomattach[k]);
System.out.println(ha.nomattach[k]);
// prep2.addBatch();
// conn.setAutoCommit(false);
// prep2.executeBatch();
// conn.setAutoCommit(true);
}
ResultSet rs = stat.executeQuery("select * from fichier;");
while (rs.next()) {
System.out.println("date_envoie = " + rs.getString("date_envoie"));
System.out.println("source = " + rs.getString("source"));
System.out.println("dest = " + rs.getString("destination"));
System.out.println("sujet = " + rs.getString("sujet"));
System.out.println("message = " + rs.getString("texte"));
System.out.println("bodyhtml = " + rs.getString("bodyhtml"));
}
rs.close();
conn.close();
}
}//fin
public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
//initialisation
FilesFinder finder = new FilesFinder();
//ouvrir boite d'envoie
finder.findFiles("C:/Boîte d'envoi");
}
} |
Partager