Home > Java > javaTutorial > body text

Write a program in Java to replace all characters in a file with '#' except specific words

WBOY
Release: 2023-09-13 08:57:17
forward
973 people have browsed it

Write a program in Java to replace all characters in a file with # except specific words

split() method of String class. Split the current string into 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 terminates at the end of the string.

replaceAll() Methods of the String class accept two strings representing regular expressions and a replacement string, and replace the matching value with the given string.

Replace all characters in the file except specific words with "#" (one way) -

  • Read the contents of the file into a string.

  • Create an empty StringBuffer object.

  • Use the split() method to split the obtained string into a String array.

  • Traverse the obtained array.

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

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

  • Finally convert StingBuffer to String.

    >

Example

Suppose we have a file named sample.txt, which contains the following content-

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

The following program reads the file content Takes a string and replaces all characters in it except specific words with "#".

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);
   }
}
Copy after login

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 ############################################
Copy after login

The above is the detailed content of Write a program in Java to replace all characters in a file with '#' except specific words. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template