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
|
package tn.com.medianet;
import java.util.*;
import java.io.*;
public class FileInput {
public static void main(String[] args) {
int v = convertToLdr("C:\\oou\\ETUDIANT.TXT", "C:\\oou\\exprt.txt");
}
public static int convertToLdr(String fileIn, String fileOut){
String exportFile = fileOut;
File file = new File(fileIn);
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
//pour mesurer la durée d'execution
Calendar m_start = new GregorianCalendar();
Calendar m_stop = new GregorianCalendar();
m_start.setTime(new Date());
try {
//fichier d'export
FileWriter fw = new FileWriter(exportFile, true);
BufferedWriter output = new BufferedWriter(fw);
fis = new FileInputStream(file);
// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
// dis.available() returns 0 if the file does not have more lines.
int i = 1;
String ligne, nom, bac, cin, prenom, date_n, sexe, delegation, gouvernorat,tour,
filiere, profil, foyer, date_limite, ligneCsv ;
while (dis.available() != 0) {
// this statement reads the line from the file and print it to
// the console.
ligne =dis.readLine();
bac = ligne.substring(0,6).trim();
cin = ligne.substring(6,14).trim();
nom = ligne.substring(14,32).trim();
prenom = ligne.substring(32,49).trim();
date_n = ligne.substring(49,57).trim();
sexe = ligne.substring(57,58).trim();
delegation = ligne.substring(58,60).trim();
gouvernorat = ligne.substring(60,62).trim();
tour = ligne.substring(62,63).trim();
filiere = ligne.substring(63,68).trim();
profil = ligne.substring(68,69).trim();
foyer = ligne.substring(69,70).trim();
date_limite = ligne.substring(70,78).trim();
ligneCsv = bac+"|"+cin+"|"+nom+ "|"+prenom+"|"+date_n+"|"+sexe+"|"+delegation+"|"+gouvernorat+
"|"+tour+"|"+filiere+ "|"+profil+"|"+"|"+foyer+"|"+date_limite+ "|"+"\n";
/* System.out.println("bac :"+bac+"-- cin :"+cin+"-- nom :"+nom+
"---prenom:"+prenom+"---date_n:"+date_n+"--sexe:"+sexe+
"---delegation:"+delegation+"--gouvernorat:"+gouvernorat);
*/
i++;
output.write("ligne :"+i+" --"+ligneCsv);
output.write(ligne+"----longueur :"+ligneCsv.length()+"\n");
}
// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
output.flush();
output.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return 0;
}
catch (IOException e) {
e.printStackTrace();
return 0;
}
m_stop.setTime(new Date());
System.out.println("Temps d'exécution : " + (m_stop.getTimeInMillis() - m_start.getTimeInMillis()) + " ms");
return 1;
}
} |
Partager