Writing List Contents to Text File After Deleting String in Java Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report To write a List<String> to a text file after deleting a specific string, you can use the following steps: Iterate through the List<String> and use the remove() method to remove the desired string.Open a FileWriter and BufferedWriter to write to the text file.Iterate through the modified List<String> and write each element to the text file using the BufferedWriter.Close the FileWriter and BufferedWriter to save the changes to the text file. Java import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.Arrays; import java.util.List; public class WriteToFile { public static void main(String[] args) { // Initialize the contents list with // the elements "apple", "banana", "cherry", "date" List<String> contents = Arrays.asList("apple", "banana", "cherry", "date"); // Initialize the stringToRemove // variable with the value "banana" String stringToRemove = "banana"; // Remove the specified string // "banana" from the contents list contents.removeIf(s -> s.equals(stringToRemove)); // Try-with-resources block to automatically handle file IO try (BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"))) { for (String line : contents) { // Write each element of the list // to a new line in the output.txt file bw.write(line); bw.newLine(); } } catch (IOException e) { // Print the stack trace // if an IO exception occurs e.printStackTrace(); } } } Here are the steps that the program follows to write the contents of a List to a text file after removing a specified string: Import the necessary classes: java.io.BufferedWriter, java.io.FileWriter, java.io.IOException, java.util.Arrays, java.util.ListInitialize the contents list with the elements you want to write to the text file, in this case it is Arrays.asList("apple", "banana", "cherry", "date").Initialize the stringToRemove variable with the string you want to remove from the contents list. In this example, it is "banana".Use the removeIf() method to remove the specified string from the contents list.Use a try-with-resources block to handle file IO. This will automatically close the file after the contents are written to it.Within the try block, use a for-each loop to iterate through the contents list.In each iteration of the loop, use the write() method of the BufferedWriter class to write the current element of the list to a new line in the text file.Use the newLine() method of the BufferedWriter class to insert a new line character after each element is written to the file.Within the catch block, use the printStackTrace() method to print the stack trace if an IO exception occurs while writing to the file.Run the code, it will create an "output.txt" file in the same directory as the class file with the elements of the List after the specified string has been removed. Output: The output of the code will be a text file named "output.txt" in the same directory as the class file. The contents of the file will be the elements of the List<String> after the specified string has been removed. For example, if the contents list contains the following strings: ["apple", "banana", "cherry", "date"] and the stringToRemove variable is set to "banana", the "output.txt" file will contain the following strings: ["apple", "cherry", "date"]. Please note that if the "output.txt" file already exists in the directory, it will be overwritten with new content. apple cherry date Comment More infoAdvertise with us Next Article Writing List Contents to Text File After Deleting String in Java Y yashtarle2171 Follow Improve Article Tags : Java Java Programs Practice Tags : Java Similar Reads Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Like