Program to replace all the characters in of a file with '#' except a particular word in Java


The split() method of the String class. splits the current string around matches of the given regular expression. The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string.

The replaceAll() method of the String class accepts two strings representing a regular expression and a replacement String and replaces the matched values with given String.

To replace all the characters in of a file with '#' except a particular word (one way) −

  • Read the contents of a file to a String.

  • Create an empty StringBuffer object.

  • Split the obtained string int o array of String using the split() method.

  • Traverse through the obtained array.

  • If any element in it matches to the required word, append it to the String buffer.

  • Replace the characters in all the remaining words with ‘#’ and append them to the StringBuffer object.

  • Finally convert the StingBuffer to String.

Example

Assume we have a file named sample.txt with the following contents −

Hello how are you welcome to Tutorialspoint we provide hundreds of technical tutorials for free.

Following program reads the contents of a file to a string replaces all the characters in it with '#' except a particular word.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class ReplaceExcept {
   public static String fileToString() throws FileNotFoundException {
      String filePath = "D://input.txt";
      Scanner sc = new Scanner(new File(filePath));
      StringBuffer sb = new StringBuffer();
      String input;
      while (sc.hasNextLine()) {
         input = sc.nextLine();
         sb.append(input);
      }
      return sb.toString();
   }
   public static void main(String args[]) throws FileNotFoundException {
      String contents = fileToString();
      System.out.println("Contents of the file: \n"+contents);
      //Splitting the words
      String strArray[] = contents.split(" ");
      System.out.println(Arrays.toString(strArray));
      StringBuffer buffer = new StringBuffer();
      String word = "Tutorialspoint";
      for(int i = 0; i < strArray.length; i++) {
         if(strArray[i].equals(word)) {
            buffer.append(strArray[i]+" ");
         } else {
            buffer.append(strArray[i].replaceAll(".", "#"));
         }
      }
      String result = buffer.toString();
      System.out.println(result);
   }
}

Output

Contents of the file:
Hello how are you welcome to Tutorialspoint we provide hundreds of technical tutorials for free.
[Hello, how, are, you, welcome, to, Tutorialspoint, we, provide, hundreds, of, technical, tutorials, for, free.]
#######################Tutorialspoint ############################################

Updated on: 14-Oct-2019

419 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements