Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method delete() is undefined for the type FileWriter

Viewed 166

I need to write a code that can delete the second text file but no matter how much I switch around or alter the code, the same error still appears

import java.io.FileWriter;
import java.io.IOException;


        public class Sample {
        public static void main(String[] args){
        String a = "";
        String b = "";
        String c = "";
        String d = "";

        try {
            FileWriter fWriter = new FileWriter("one.txt");

            fWriter.write(a);
            System.out.println(a);
            fWriter.write(b);
            System.out.println(b);
            fWriter.close();

            FileWriter fWriters = new FileWriter("two.txt");

            fWriters.write(c);
            System.out.println(c);
            fWriters.write(d);
            System.out.println(d);
            fWriters.close();

            if (fWriters.delete()) { 
                System.out.println("Deleted the file");
        }
    }
            

        catch (IOException e) {
            System.out.print(e.getMessage());

        }

        }}
0 Answers
Related