|
|
|
|
Bonjour,
j'ai un soucis en Java. Je dois rechercher dans un texte des mots clés du genre "error(param1,param2,...) ou encore Write(param1,param2,...) et à chaque fois que j'en trouve un, je dois rajouter dans le code un Tag juste avant la ligne trouvée. (ex: ** TAG** méthode(param1,param2...) et ensuite mettre en commentaire la ligne où il y avait le mot clé.
Ca fait un moment que je cherche mais je ne trouve pas la solution. Quelqu'un pourrait m'aider?
Merci d'avance.
Configuration: Windows XP Firefox 2.0.0.11
Bonjour,
public static void FindAndUpdate ( String theInputFileName, String theOutputFileName, String theSymbol, String theCommentSymbol, String theExpression ){
int myStartIndex = -1;
String myTmpStr = null;
String myOutputString = "";
boolean found = false;
RandomAccessFile myInputFile = null;
RandomAccessFile myOutputFile = null;
String myLine = null;
try{
// --- Open the file
myInputFile = new RandomAccessFile(theInputFileName, "r");
myOutputFile = new RandomAccessFile(theOutputFileName, "rw");
// --- Read line per line
while ( (myLine = myInputFile.readLine() ) != null ){
// --- Init the index for the new line
myStartIndex = -1;
found = false;
myOutputString = "";
myTmpStr = myLine;
// --- And for each line, search the pattern 'theSymbol' and try to comment it
while ( myTmpStr != "" && (myStartIndex = myTmpStr.indexOf(theSymbol)) != -1 ){
found = true;
myOutputString = myTmpStr.substring(0, myStartIndex).concat(theExpression).concat( theSymbol );
myTmpStr = myTmpStr.substring( myStartIndex + theSymbol.length(), myTmpStr.length() );
}
// --- Add the end of the line
if ( myTmpStr != "" )
myOutputString += myTmpStr;
// --- If the tag had been found, comment the line (add '//' at the beginning of the line
if ( found )
myOutputString = "\n".concat(theCommentSymbol).concat(myOutputString);
else
// --- No symbol found, so just write the same line
myOutputString = "\n".concat(myLine);
// --- Finally add this new line to the output file
myOutputFile.write( myOutputString.getBytes());
}
}catch (IOException e){
System.err.println("IOException : "+e.getMessage());
}finally {
// --- Close the openned files
try{
myInputFile.close();
myOutputFile.close();
}catch ( Exception e ){
; // --- Do nothing
}
}
}
Utilisation :
public static void main ( String[] args ){
String myInputFile = "C:\\CAHDD\\test\\exemple.txt";
String myOutputFile = "C:\\CAHDD\\test\\exemple2.txt";
String theSymbol = "export(";
String theCommentSymbol = "//";
String theTag = "<ICI>";
FindAndUpdate(myInputFile, myOutputFile,theSymbol, theCommentSymbol, theTag);
}
Avec dans le fichier texte 'exemple.txt', plusieurs ligne de texte dont certaine contienne l'expression "export(". C'est un exemple, ca fonctionne, reste plus qu'à l'adapter à tes besoins. ~ N'oubliez pas la balise "Résolu" lorsque votre problème est... résolu :) ~ |