[debutant]erreur java unreported exception java.io.IOException
Bonjour,
donc mon programme parcours un répertoire et récupere le fichier txt du jour
a partir de ce fichier txt je dois créer un fichier csv avec certaines données du fichier txt
pour la 1ere partie parcourir le repertoire et recuperer le nom du fichier txt c'est ok
ensuite je lis une 1ere fois le fichier txt pour compter le nb ligne qui contient un mot particulier pas toutes les lignes sont concernés et en suite ecrire l'entete de mon csv avec le compteur c'est ok ca fonctionne
ensuite lis une 2eme fois mon fichier txt pour ecrire les lignes que je veux dans mon fichier csv mon la j'ai un soucis
j'ai le message suivant :
unreported exception java.io.IOException; must be caught or declared to be thrown
ligne2=tampon2.readLine();
pourtant justement j'ai utilisé 2 filereader et 2 tampons
je ne comprends pourquoi j'ai cette erreur
mon code:
Code:
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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
public class MyProgCSV {
public static String namecsvfile="";
public static void main(String args[]){//void main
//--------------------------------variables--------------------------------------------------------//
//----variables part1-----------------------------//
String mytxtfolder="X:\\Sjs\\prd\\rprts";
String mytxtfile="";
String filename;
int i=0;
int mycounter=0;
long timestamp;
Date mytimestamp;
Locale locale;
DateFormat dateFormat;
GregorianCalendar todaydate;
Date mycurrentdate;
String date1;
String date2;
//-----variables part2--------------------------//
FileReader monfichier=null;
BufferedReader tampon=null;
FileReader monfichier2=null;
BufferedReader tampon2=null;
String mycsvfolder="D:\\";
String ligne;
String ligne2;
String eder;
String edera;
String ederb;
String eder1;
String eder2;
String OrigFileNamea;
String ligne1csv;
String ligne2csv;
String ligne3csv;
String noticeid;
String noticeid1;
String noticeid2;
String mytype;
String pubdate;
String OrigLang;
String OrigFilename;
String pathtempcsv=mycsvfolder+"\\myfile.csv";
Write mywriter=new Write();
int j=0;
int k=0;
int counter=0;
//------------------------------------------------------------------------------------------------//
//------part1-------debut parcourir le repertoire et trouve le fichier du jour-------------------------//
try{//try1
File myrep = new File(mytxtfolder);
FileFilter filefilter=new FileFilter(){
public boolean accept(File file){
return file.isFile();
}
};
File[] mylistfiles=myrep.listFiles(filefilter);
if(mylistfiles==null){//if1
System.out.println("rep n existe pas");
}else{
for(i=0;i<mylistfiles.length;i++){//for1
if(mylistfiles[i].isFile()){//if2
timestamp=mylistfiles[i].lastModified();
mytimestamp=new Date(timestamp);
locale=Locale.getDefault();
dateFormat=DateFormat.getDateInstance(DateFormat.SHORT, locale);
todaydate=new GregorianCalendar();
mycurrentdate=todaydate.getTime();
date1=dateFormat.format(mytimestamp);
date2=dateFormat.format(mycurrentdate);
if (date1.compareTo(date2)==0){//if3
filename=mylistfiles[i].toString();
mytxtfile=filename;
mycounter=mycounter++;
if(mycounter>1){//if4
i=mylistfiles.length;
}//if4
}//if3
}//if2
}//for1
}//if1
}catch(Exception ioe){//try1
ioe.printStackTrace();
}//try1
//------part1-------fin parcourir le repertoire et trouve le fichier du jour-------------------------//
//mon fichier a traite est :
System.out.println(mytxtfile);
//----part2---------debut creation du fichier csv----------------------------------------------------//
//-------1)debut compteur -----//
try{//try2
monfichier=new FileReader(mytxtfile);
tampon=new BufferedReader(monfichier);
monfichier2=new FileReader(mytxtfile);
tampon2=new BufferedReader(monfichier2);
while(true){//while1
ligne=tampon.readLine();
//System.out.println(ligne);
if (ligne==null){ break;}
String[] tab_str=ligne.split("\\|");
if(j>2){//if5
OrigFileNamea=tab_str[25];
edera=OrigFileNamea.substring(50, 56);
ederb=OrigFileNamea.substring(59,65);
if(edera.equals("mytext")||ederb.equals("mytext")){//if6
counter++;
}//if6
}//if5
j++;
}//while1
}catch(Exception exception){//try2
exception.printStackTrace();
}finally{//try2
try{//try3
tampon.close();
monfichier.close();
}catch(Exception exception1){//try3
exception1.printStackTrace();
}//try3
}//try2
//-------1)fin compteur simap2-----//
//mon compteur simap2 est :
System.out.println(counter);
//-------2) debut creation fichier csv------------------------//
ligne1csv="### release: 0.1 (date: 30-jan-2006) ###";
ligne2csv="### eder = mytext ### nbr of doc: "+counter+" ###";
ligne3csv="eder|Notice_ID|Pub|Doc Number|Lang";
mywriter.ecrire(ligne1csv+"\n", pathtempcsv);
mywriter.ecrire(ligne2csv+"\n", pathtempcsv);
mywriter.ecrire(ligne3csv+"\n", pathtempcsv);
while(true){//while2
ligne2=tampon2.readLine();
//System.out.println(ligne2);
if (ligne2==null){ break;}
String[] tab_str2=ligne2.split("\\|");
if(k>2){//if7
noticeid=tab_str2[0];
pubdate=tab_str2[13];
OrigLang=tab_str2[19];
OrigFilename=tab_str2[25];
//eder
eder1=OrigFilename.substring(50, 56);
eder2=OrigFilename.substring(59, 65);
if(eder1.equals("mytext")||eder2.equals("mytext")){//if8
eder="mytext";
}//if8
//NoticeID
}//if7
}//while2
//-------2) fin creation fichier csv------------------------//
//-----part2--------fin creation du fichier csv----------------------------------------------------//
}//void main
}//class MyProgCSV
class Write
{ //class write
public void ecrire(String texte,String pathmyfile)
{ //void ecrire
//String mypathfile ="d:\\myfile.csv";
try { //try1
FileWriter fw = new FileWriter(pathmyfile, true);
BufferedWriter output = new BufferedWriter(fw);
output.write(texte);
output.flush();
output.close();
}catch(IOException ioe){//try1
System.out.println("erreur : " + ioe );
} //try1
} //void ecrire
} //class write |
merci pour votre aide
;)