Advertisement
brilliant_moves

ReplaceText.java

Sep 15th, 2013
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.58 KB | None | 0 0
  1. import java.io.*;       // for file handling
  2. import java.util.Scanner;   // for user input
  3.  
  4. public class ReplaceText {
  5.  
  6.     /**
  7.     *   Program:    ReplaceText.java
  8.     *   Creator:    Chris Clarke
  9.     *   Created:    18.10.2012
  10.     *   Purpose:    Open text file, replace <oldtext> with <newText>, save.
  11.     */
  12.  
  13.     public static void main(String[] args) {
  14.         String oldText, newText;
  15.         String allText;
  16.         String fileName, backupName;
  17.         String backup;
  18.  
  19.         Scanner scan = new Scanner(System.in);
  20.         System.out.print("Enter filename, including path: ");
  21.         fileName = scan.nextLine();
  22.  
  23.         if (fileName.equals("")) {
  24.             System.out.println("No file!");
  25.             return;
  26.         } // end if
  27.  
  28.         System.out.print("Enter text to be replaced: ");
  29.         oldText = scan.nextLine();
  30.  
  31.         if (oldText.equals("")) {
  32.             System.out.println("No text entered!");
  33.             return;
  34.         } // end if
  35.  
  36.         System.out.print("Enter replacement text: ");
  37.         newText = scan.nextLine();
  38.  
  39.         System.out.println();
  40.  
  41.         allText = readFromFile(fileName);
  42.  
  43.         System.out.print("Make backup (y/n)? ");
  44.         backup = scan.nextLine();
  45.         if (backup.toLowerCase().startsWith("y")) {
  46.             System.out.print("Enter backup filename, including path: ");
  47.             backupName = scan.nextLine();
  48.             writeToFile(backupName, allText);
  49.         } // end if
  50.  
  51.         allText = allText.replaceAll(oldText, newText);
  52.         writeToFile(fileName, allText);
  53.  
  54.     } // end main
  55.  
  56.     private static String readFromFile(String fileName) {
  57.         String text = "";
  58.         try {
  59.             // Create the readers.
  60.             FileReader fr = new FileReader(fileName);
  61.             LineNumberReader lnr = new LineNumberReader(fr);
  62.  
  63.             // Read line by line
  64.             String s = " "; // Anything but null.
  65.             while (s != null) {
  66.                 s = lnr.readLine();
  67.                 if (s == null) break;
  68.                 System.out.println(s); // option to display text
  69.                 text += s+"\r\n"; // add line of text plus newline
  70.             } // end while
  71.             // Close the readers.
  72.             lnr.close();
  73.             fr.close();
  74.         }
  75.         catch (IOException x) {
  76.             System.out.println("Trouble reading! " + x.getMessage());
  77.         } // end try...catch
  78.  
  79.         return text;
  80.  
  81.     } // end readFromFile
  82.  
  83.     private static void writeToFile(String fileName, String text) {
  84.         if (text.equals("")) return;
  85.         try {
  86.             // Create the writer.
  87.             BufferedWriter fw = new BufferedWriter(new FileWriter(fileName));
  88.             for (int i=0; i<text.length(); i++) {
  89.                 System.out.print(text.charAt(i)); // option to display text
  90.                 fw.write(text.charAt(i));
  91.             } // end for
  92.             fw.close();
  93.             System.out.println("File saved.");
  94.         }
  95.         catch (IOException x) {
  96.             System.out.println("Trouble writing! " + x.getMessage());
  97.         } // end try...catch
  98.     } // end writeToFile
  99. } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement