Search TXT Formats in Java
Native and high performance TXT file search using Java APIs, without the use of any software like Microsoft or Adobe PDF.
How to Search TXT File Using Java
In order to search TXT file, we’ll use
API which is a feature-rich, powerful and easy to use Search API for Java platform. You can download its latest version directly from
and install it within your Maven-based project by adding the following configurations to the pom.xml.
<repository> <id>AsposeJavaAPI</id> <name>Aspose Java API</name> <url>https://repository.aspose.com/repo/</url> </repository>
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-words</artifactId> <version>version of aspose-words API</version> <classifier>jdk17</classifier> </dependency>
Steps to Search TXT Files in Java
Developers can easily integrate code with just few lines as listed.
- Load TXT file by instantiating Document Class object.
- Instantiate FindReplaceOptions.
- Use Pattern.compile() method to define a regex pattern
- Use getRange().replace method to find and replace
- Save TXT file.
Before integrating the code, make sure that you have the following prerequisites.
- Microsoft Windows or a compatible OS with Java Runtime Environment for JSP/JSF Application and Desktop Applications.
- Get latest version of Aspose.Words for Java directly from Maven .
Search TXT Files - Java
// Load TXT file Document txt = new Document("sourceFile.txt"); // Find and replace similar pattern words in the file FindReplaceOptions options = new FindReplaceOptions(); txt.getRange().replace(Pattern.compile("[B|S|M]ad"), "[replaced]", options); // Save the TXT file txt.save("output.txt");
Online TXT Search Live Demos
TXT What is TXT File Format
A file with .TXT extension represents a text document that contains plain text in the form of lines. Paragraphs in a text document are recognized by carriage returns and are used for better arrangement of file contents. A standard text document can be opened in any text editor or word processing application on different operating systems. All the text contained in such a file is in human-readable format and represented by sequence of characters.Read More
Other Supported Search Documents
Using Java, one can also search other files including.