rtfmplay Posted July 3, 2008 Report Share Posted July 3, 2008 Salut, sunt probabil pe aici oameni care cunosc java foarte bine, cu aceasta ocazie as dori niste informatii sau putin ajutor la un mic script. As vrea sa creez un scrip care sa deschida un anume fisier "c:\test.txt" si sa mearga la o anumita linie si sa inlocuiasca textul de acolo cu un alt text.Multumesc anticipat.test.txt------------------1234 Hello (sa fie inlocuit cu goodbye)56 Quote Link to comment Share on other sites More sharing options...
Vhaerun Posted July 4, 2008 Report Share Posted July 4, 2008 import java.io.*;public class FileTest { public static void main(String[] args) throws Exception { System.out.println(args.length); if(args.length != 3) { System.out.println("Mod de folosire:java FileTest fisier linie inlocuitor"); } File f1 = new File(args[0]); File f2 = new File(args[0]+".bak"); BufferedReader br = new BufferedReader(new FileReader(f1)); PrintWriter pw = new PrintWriter(f2); int linie = Integer.parseInt(args[1]); int contor = 1; String inlocuitor = args[2]; String linieC = ""; while((linieC = br.readLine()) != null) { if(contor == linie) { pw.println(inlocuitor); contor++; continue; } contor++; pw.println(linieC); } br.close(); pw.close(); f1.delete(); f2.renameTo(f1); }} Quote Link to comment Share on other sites More sharing options...
Grunt Posted July 4, 2008 Report Share Posted July 4, 2008 if(contor == linie)Ar trebui sa fie: if (contor.equalsTo(linie)) Quote Link to comment Share on other sites More sharing options...
kw3rln Posted July 4, 2008 Report Share Posted July 4, 2008 if(contor == linie)Ar trebui sa fie: if (contor.equalsTo(linie))da .. ca contor == linie returneaza false din cauza cas diferite adresele variabilelor. nu compara valoarea variabilelor ci adresa lor in memorie care ii diferitabv pt cod Quote Link to comment Share on other sites More sharing options...
Vhaerun Posted July 4, 2008 Report Share Posted July 4, 2008 int nu e obiect . Quote Link to comment Share on other sites More sharing options...
rtfmplay Posted July 4, 2008 Author Report Share Posted July 4, 2008 Vhaerun, Grunt, kw3rln va multumesc pentru ajutorul oferit. Raman dator cu un serviciu in limita puterilor si cunostinetelor mele! Va urez o zi buna! Quote Link to comment Share on other sites More sharing options...