Jump to content
rtfmplay

Putin ajutor in java

Recommended Posts

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

------------------

1

2

3

4 Hello (sa fie inlocuit cu goodbye)

5

6

Link to comment
Share on other sites


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);

}
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...