
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to delete a string inside a file(.txt) in java?
The replaceAll() method accepts a regular expression and a String as parameters and, matches the contents of the current string with the given regular expression, in case of match, replaces the matched elements with the String.
To delete a particular String from a file using the replaceAll() method −
Retrieve the contents of the file as a String.
Replace the required word with an empty String using the replaceAll() method.
Rewrite the resultant string into the file again.
Example
import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; public class StringExample { public static String fileToString(String filePath) throws Exception{ String input = null; Scanner sc = new Scanner(new File(filePath)); StringBuffer sb = new StringBuffer(); while (sc.hasNextLine()) { input = sc.nextLine(); sb.append(input); } return sb.toString(); } public static void main(String args[]) throws FileNotFoundException { String filePath = "D://sample.txt"; String result = fileToString(filePath); System.out.println("Contents of the file: "+result); //Replacing the word with desired one result = result.replaceAll("\bTutorialspoint\b", ""); //Rewriting the contents of the file PrintWriter writer = new PrintWriter(new File(filePath)); writer.append(result); writer.flush(); System.out.println("Contents of the file after replacing the desired word:"); System.out.println(fileToString(filePath)); } }
Output
Contents of the file: Hello how are you welcome to Tutorialspoint Contents of the file after replacing the desired word: Hello how are you welcome to
- Related Articles
- How to overwrite a line in a .txt file using Java?
- How to read a .txt file with RandomAccessFile in Java?
- Can we use readUTF() to read a string from a .txt file in Java?
- How to make a txt file and read txt file from internal storage in android?
- How to store list in a txt file and read list from txt file in android?
- How to delete a temporary file in Java?
- How to make a txt file in internal storage in android?
- How to search a value inside a JSON file using Jackson in Java?
- Delete Lines in a Text File That Contain a Specific String
- How to delete a file using Python?
- Java program to delete certain text from a file
- How to make a txt file in external storage with runtime permission in android?
- How to read a txt file in external storage with runtime permission in android?
- Plot data from a .txt file using matplotlib
- How to extract all the .txt files from a zip file using Python?

Advertisements