Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Find Whether a Word Exists in a File or Not?

Find Word in File Example

Condition for Finding a Word in a File

  • Description: To determine whether a word exists in a file, the content of the file must be read and searched for the word. This can be accomplished by opening the file using classes like FileReader and BufferedReader or using Files.readAllLines() for simpler file reading. After reading the file line by line, each line can be checked to see if it contains the specified word. The `contains()` method of the String class helps in performing this check. If the word is found, a boolean value or message indicating the presence of the word can be returned. Efficient searching can also be achieved by utilizing regular expressions with the Pattern class for more complex matching. The process stops once the word is found or the entire file is read.
Sample Source Code
  • # WordExist.java
    package JavaSamples;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import javax.swing.JOptionPane;

    public class WordExist extends javax.swing.JFrame {
    public WordExist() {
    initComponents();
    }

    @SuppressWarnings("unchecked")
    private void initComponents() {
    // UI setup code
    }
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    javax.swing.JFileChooser fileChooser = new javax.swing.JFileChooser();
    fileChooser.setDialogTitle("Choose a file");
    int result = fileChooser.showOpenDialog(this);
    if (result == javax.swing.JFileChooser.APPROVE_OPTION) {
    File selectedFile = fileChooser.getSelectedFile();
    jTextField1.setText(selectedFile.getAbsolutePath());
    } else {
    jTextField1.setText("No File Selected ");
    }
    }
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    String filePath = jTextField1.getText();
    String wordToFind = jTextArea1.getText();
    boolean found = false;
    try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
    String line;
    while ((line = reader.readLine()) != null) {
    String[] words = line.split("\\s+");
    for (String word : words) {
    if (word.equals(wordToFind)) {
    found = true;
    break;
    }
    }
    if (found) {
    break;
    }
    }
    }
    } catch (Exception e) {
    System.err.println(e.getMessage());
    }
    if (found) {
    JOptionPane.showMessageDialog(null, "The word \"" + wordToFind + "\" exists in the file.");
    } else {
    JOptionPane.showMessageDialog(null, "The word \"" + wordToFind + "\" does not exist in the file.");
    }
    }
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(() -> {
    new WordExist().setVisible(true);
    });
    }
    }
Screenshots
  • STEP 1: The user selects a text document from local files.
  • Select File Dialog

  • STEP 2: After clicking the "Choose File" button, the file path will be displayed in the text field.
  • Display File Path

  • STEP 3: The user inputs the word to search and clicks the "Check" button to find it in the file.
  • Word Search

  • STEP 4: The result shows whether the word exists in the file or not.
  • Word Exists Result