public static void main(String args[])
{
int p = 0;
int nb = 0;
int indx = 1;
int repl = 0;
String rech;
String remp;
try { nb = Integer.parseInt(args[0]); }
catch (Throwable t) { //bouh }
for (int i = nb * 2 + 1; i < args.length; i++)
{
try
{
RandomAccessFile raf = new RandomAccessFile(args[i], "r");
byte b[] = new byte[new Long(raf.length()).intValue()];
raf.read(b);
raf.close();
File fichier = new File(args[i]);
String s = new String(b);
for (int j = 2; j < nb * 2 + 1; j += 2)
{
p = 0;
repl = 0;
rech = args[j - 1];
remp = args[j];
while (p != -1)
{
p = s.indexOf(rech, p);
if (p != -1)
{
s = s.substring(0, p) + remp + s.substring(p + rech.length());
repl++;
}
}
}
try
{
fichier.delete();
RandomAccessFile rf = new RandomAccessFile(args[i], "rw");
rf.write(s.getBytes());
rf.close();
}
catch (IOException e)
{
System.exit(0);
}
}
catch (IOException e)
{
System.exit(0);
}
}
}
}
la syntaxe c'est
foo [nombres de mot a chercher] [mot 1] [remplacement_mot 1] ... [mot n] [remplacement_mot n] [fichier 1] ... [fichier n]
[^] # Re: en java bien sûr !
Posté par cho7 (site web personnel) . En réponse au message Question: Quel langage?. Évalué à 2.
import java.io.*;
public class Foo
{
public static void main(String args[])
{
int p = 0;
int nb = 0;
int indx = 1;
int repl = 0;
String rech;
String remp;
try { nb = Integer.parseInt(args[0]); }
catch (Throwable t) { //bouh }
for (int i = nb * 2 + 1; i < args.length; i++)
{
try
{
RandomAccessFile raf = new RandomAccessFile(args[i], "r");
byte b[] = new byte[new Long(raf.length()).intValue()];
raf.read(b);
raf.close();
File fichier = new File(args[i]);
String s = new String(b);
for (int j = 2; j < nb * 2 + 1; j += 2)
{
p = 0;
repl = 0;
rech = args[j - 1];
remp = args[j];
while (p != -1)
{
p = s.indexOf(rech, p);
if (p != -1)
{
s = s.substring(0, p) + remp + s.substring(p + rech.length());
repl++;
}
}
}
try
{
fichier.delete();
RandomAccessFile rf = new RandomAccessFile(args[i], "rw");
rf.write(s.getBytes());
rf.close();
}
catch (IOException e)
{
System.exit(0);
}
}
catch (IOException e)
{
System.exit(0);
}
}
}
}
la syntaxe c'est
foo [nombres de mot a chercher] [mot 1] [remplacement_mot 1] ... [mot n] [remplacement_mot n] [fichier 1] ... [fichier n]
Ya pas de gestion d'erreur.