0

Not deleting .txt file for some reason

code: import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter Path (dont forget to escape characters!):"); String path = scan.nextLine(); File file = new File(path); try{ boolean isGood = file.createNewFile(); if (isGood){ FileWriter write = new FileWriter(path); System.out.println("Write to file:"); write.write(scan.nextLine()); write.close(); System.out.println("Content in file after writing to it:"); Scanner read = new Scanner(file); if (read.hasNextLine()){ System.out.println(read.nextLine()); } System.out.println("Would You like to delete it? 'yes' or 'no'"); String yesOrNo = scan.next(); boolean delete = yesOrNo.equalsIgnoreCase("yes") ? true : false; if (delete){ if (file.delete()){ System.out.println("File has been deleted!"); }else{ System.out.println("Couldnt delete file for somereason..."); } }else{ System.out.println("File is NOT deleted!"); } }else{ System.out.println("File already exists!"); } }catch(IOException e){ e.fillInStackTrace(); } } }

14th Dec 2020, 3:04 PM
Yahel
Yahel - avatar
2 Réponses
0
Please add a MWE and not write your code like this https://www.sololearn.com/post/75089/?ref=app
14th Dec 2020, 4:25 PM
Krish
Krish - avatar