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
| import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TraitementFichier {
public static void main(String[] args) throws IOException {
try {
File f = new File("/home/user/Bureau/Exportclient.txt");
BufferedReader b = new BufferedReader(new FileReader(f));
String readLine = "";
System.out.println("Lecture du fichier source");
while ((readLine = b.readLine()) != null) {
if (readLine.trim().isEmpty()) continue;
constFichier client = new constFichier();
client.journal = readLine.substring(5, 7).trim() + ";";//journal
client.jour = readLine.substring(13, 15).trim() + "/"; //le jour
client.mois = readLine.substring(11, 13).trim() + "/"; //Le mois
client.annee = readLine.substring(07, 11).trim() + ";"; //L'année
client.reference = readLine.substring(35, 46).replace(" ", "").trim() + ";";//reference
client.libelle = readLine.substring(46, 69).trim() + ";";//libellé
client.montant = readLine.substring(75, 85).replace(" ", "").trim() + ";"; //Montant
client.beneficiare = readLine.substring(103, 136) + "\r\n";//beneficiaires
String deb=new String(client.montant);
Pattern p=Pattern.compile("\\d.+D;");
Matcher m=p.matcher(deb);
while(m.find()) {
client.debit=m.group();
client.debi=new
String(";"+client.montant).replace("D","");
}
String cred=new
String(client.montant);
Pattern pcred=Pattern.compile("\\d.+C;");
Matcher mcred=pcred.matcher(cred);
while(mcred.find()) {
client.credi=new String(client.montant+";").replace("C","");
}
String newfich=new String(client.journal+client.jour+client.mois+client.annee+client.reference+client.credi+client.debi+"\r\n");
File fichcsv = new File("exportCSV.csv");
if (!fichcsv.exists()) {
fichcsv.createNewFile();
}
FileWriter fw = new FileWriter(fichcsv.getAbsoluteFile(), true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(newfich);
bw.close();
System.out.println(newfich);
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("modif terminé");
}
} |