Advertisement
romancha

FileFourth

Mar 14th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Scanner;
  5.  
  6. public class ReplacementInFile {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         System.out.println("Enter java file path:");
  10.         String path = scanner.nextLine();
  11.         List<String> fileLines = new ArrayList<>();
  12.  
  13.         try(BufferedReader reader = new BufferedReader(new FileReader(path))) {
  14.             String line;
  15.             while ((line = reader.readLine()) != null) {
  16.                 if(line.contains("public")) {
  17.                     fileLines.add(line.replace("public", "private"));
  18.                 } else {
  19.                     fileLines.add(line);
  20.                 }
  21.             }
  22.  
  23.             BufferedWriter writer = new BufferedWriter(new FileWriter(path));
  24.             for (String fileLine : fileLines) {
  25.                writer.write(fileLine + "\n");
  26.             }
  27.             writer.close();
  28.         } catch (IOException e) {
  29.             e.printStackTrace();
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement