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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
| import java.awt.event.*;
import java.io.*;
import javax.swing.JOptionPane;
import javax.swing.JFileChooser;
class convertion implements ActionListener {
public void actionPerformed(ActionEvent contexte) {
// TODO Auto-generated method stub
String caractere=" CIE AV MV JV NUVOL DEP ARR";
String tab_origin;
String nom_fichier=null; //nom du fichier à ouvrir
String nom_fichier2=null; //nom du nouveau fichier qui sera créer
String nom="_traité.txt"; //nouvelle extension du fichier créer
boolean premierefois=true; //servira pour savoir si c'est la premiere fois que l'on écrit dans le nouveau fichier afin de coller avant le traitement les caractères CIE AV ...
//traitement pour offrir à l'utilisateur la possibilité de parcourir des dossiers afin de trouver le fichier à convertir
String wd = System.getProperty("user.dir");
JFileChooser fc = new JFileChooser(wd);
int rc = fc.showDialog(null, "Choisir le fichier");
if (rc == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
nom_fichier = file.getAbsolutePath();
}
//manipulation pour créer le nom du nouveau fichier
nom_fichier2=nom_fichier.substring(0,nom_fichier.length()-4);
nom_fichier2=nom_fichier2.concat(nom);
//BufferedReader reader = new BufferedReader(new InputStreamReader( nom_fichier );
try
{
//String nomFichier = reader.readLine();
FileReader fr = new FileReader( nom_fichier );
BufferedReader br = new BufferedReader( fr );
String ligne = null;
//création du nouveau fichier
File fichier=new File(nom_fichier2);
fichier.createNewFile();
while( ( ligne = br.readLine() ) != null )
{
tab_origin=ligne;
//affectation de chaque ligne du fichier dans un tableau char pour pouvoir traiter l'information
char tab[]=new char[tab_origin.length()];
for (int i=0; i<tab_origin.length();i++)
{
tab[i]=tab_origin.charAt(i);
}
char tab_final[]=new char[34];//création d'un tableau qui contiendra la ligne traité,tableau que l'on ecrira directement dans le nouveau fichier
tab_final[0]=' ';
tab_final[1]=tab[22];
tab_final[2]=tab[23];
tab_final[3]=' ';
tab_final[4]=' ';
tab_final[5]=' ';
tab_final[6]=' ';
tab_final[7]=tab[25];
tab_final[8]=tab[26];
tab_final[9]=' ';
tab_final[10]=tab[27];
tab_final[11]=tab[28];
tab_final[12]=' ';
tab_final[13]=tab[29];
tab_final[14]=tab[30];
tab_final[15]=' ';
tab_final[16]=' ';
tab_final[17]=' ';
tab_final[18]=tab[32];
tab_final[19]=tab[33];
tab_final[20]=tab[34];
tab_final[21]=tab[35];
tab_final[22]=' ';
tab_final[23]=' ';
tab_final[24]=' ';
tab_final[25]=' ';
tab_final[26]=tab[36];
tab_final[27]=tab[37];
tab_final[28]=tab[38];
tab_final[29]=' ';
tab_final[30]=' ';
tab_final[31]=tab[39];
tab_final[32]=tab[40];
tab_final[33]=tab[41];
//préparation pour ecrire le tab_final dans le fichier traité
PrintWriter ecrivain;
//if(fichier.exists()==false)
// {
try {
if(premierefois==true)//si c est la premiere fois que l'on écrit il faudra mettre au début du fichier CIE AV MV...
{
premierefois=false;
//traitement pour pouvoir écrire directement CIE MV... dans le fichier
char caract[]=new char[caractere.length()];
for (int i=0; i<caractere.length();i++)
{
caract[i]=caractere.charAt(i);
}
ecrivain=new PrintWriter(new BufferedWriter (new FileWriter(nom_fichier2,true)));
ecrivain.println(caract);
ecrivain.println(tab_final);
ecrivain.close();
JOptionPane.showMessageDialog(null,
"Convertion et création du fichier avec succès(NOM_traité.txt) !", "convertion",
JOptionPane.INFORMATION_MESSAGE);
}
else
{
ecrivain=new PrintWriter(new BufferedWriter (new FileWriter(nom_fichier2,true)));
ecrivain.println(tab_final); //on écrit le tableau qui contient les données traitées dans chaque ligne du fichier
ecrivain.close();
}
}
catch (IOException e) {
// TODO Auto-generated catch block
System.out.println( "Erreur lors de la création du fichier");
}
}//fin de la boucle while qui fait le traitement ligne par ligne du fichier original
br.close();
}
catch( IOException e )
{
JOptionPane.showMessageDialog(null,
"Problème inconnue lors de la lecture du fichier", "convertion",
JOptionPane.ERROR_MESSAGE);
}
System.exit(0);
}
} |
Partager