Advertisement
Guest User

deleteOnExit does not work for folders

a guest
May 20th, 2017
233
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.PrintStream;
  5.  
  6. public class TryJavaFileDeleteOnExit {
  7.  
  8.     public static void main(String[] args) {
  9.         try {
  10.             File tempFolder = File.createTempFile("myTempFolder", "");
  11.             tempFolder.delete();
  12.             tempFolder.mkdirs();
  13.            
  14.             // deleteOnExit() does NOT work, temp-folder and files will NOT be deleted
  15.             tempFolder.deleteOnExit();
  16.            
  17.             System.out.println("tempFolder path = " + tempFolder.getAbsolutePath());
  18.            
  19.             for(int i=1; i<=3; i++) {
  20.                 File fileInTempFolder = new File(tempFolder, "fileInTempFolder_" + i + ".txt");
  21.                 PrintStream fileOut = new PrintStream(new FileOutputStream(fileInTempFolder));
  22.                 fileOut.println("hell0!");
  23.                 fileOut.close();
  24.             }
  25.         } catch (IOException e) {
  26.             e.printStackTrace();
  27.         }
  28.        
  29.     }
  30. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement