Java text file replace text

Description

Java text file replace text

import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) throws Exception {
    File sourceFile = new File("Main.java");
    File targetFile = new File("Main1.java");
    try (Scanner input = new Scanner(sourceFile); 
         PrintWriter output = new PrintWriter(targetFile);) {
      while (input.hasNext()) {
        String s1 = input.nextLine();
        String s2 = s1.replaceAll("a", "newA");
        output.println(s2);//from   w  w  w  . j a va2s. c o  m
      }
    }
  }
}
 



PreviousNext

Related