Open In App

Writing List Contents to Text File After Deleting String in Java

Last Updated : 16 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

To write a List<String> to a text file after deleting a specific string, you can use the following steps:

  1. Iterate through the List<String> and use the remove() method to remove the desired string.
  2. Open a FileWriter and BufferedWriter to write to the text file.
  3. Iterate through the modified List<String> and write each element to the text file using the BufferedWriter.
  4. 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:

  1. Import the necessary classes: java.io.BufferedWriter, java.io.FileWriter, java.io.IOException, java.util.Arrays, java.util.List
  2. Initialize 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”).
  3. Initialize the stringToRemove variable with the string you want to remove from the contents list. In this example, it is “banana”.
  4. Use the removeIf() method to remove the specified string from the contents list.
  5. Use a try-with-resources block to handle file IO. This will automatically close the file after the contents are written to it.
  6. Within the try block, use a for-each loop to iterate through the contents list.
  7. 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.
  8. Use the newLine() method of the BufferedWriter class to insert a new line character after each element is written to the file.
  9. Within the catch block, use the printStackTrace() method to print the stack trace if an IO exception occurs while writing to the file.
  10. 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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads