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 Program to Read Content From One File and Write it into Another File File handling plays a major role in doing so as the first essential step is writing content to a file. For this is one must know how to write content in a file using the  FileWriter class. The secondary step is reading content from a file and print the same. For this, one must have good hands on Fil 5 min read How to Write an ArrayList of Strings into a Text File? In Java, ArrayList is a class in the java.util package. It facilitates the creation of dynamic arrays that can dynamically grow or shrink as elements are added or removed. This class is part of the Java Collections Framework. An ArrayList of strings is converted into a text file and implemented usin 2 min read Java Program to Create String from Contents of a File A File is a computer resource which is deployed to store different types of data such as text, image, video, to name a few. It is basically a collection of data bound to a single entity. While using your computer, it becomes essential to be able to deal with files and in this article we will be lear 6 min read Files Class writeString() Method in Java with Examples The writeString() method of File Class in Java is used to write contents to the specified file. Syntax: Files.writeString(path, string, options) Parameters: path - File path with data type as Pathstring - a specified string which will enter in the file with return type String.options - Different op 2 min read Java Program to Write into a File FileWriter class in Java is used to write character-oriented data to a file as this class is character-oriented because it is used in file handling in Java. There are many ways to write into a file in Java as there are many classes and methods which can fulfill the goal as follows:Using writeString( 5 min read Like