• 0

[JAVA] Delete temporary files when...


Question

I am working on a page for my employer and I need a little help if you don't mind :D

Right now there is an application running that sends a bunch of charcters to a JSP page when a button is clicked (that is the best description of that). What I do is grab those characters and create a new temporary file so the person who clicked the button can download it.

Here is what I do:

...
File dir = (File) getServletContext().getAttribute("javax.servlet.context.tempdir");
File temp = File.createTempFile(item.getName(), "", dir);
temp.deleteOnExit();
...
while((line = in.readLine()) != null)
	fout.write(line + "\r\n");
fout.close();
...

Everything works GREAT. Yippy, I am happy...

BUT this is my issue. The file is no longer temporary. The file will stick around until the JVM is killed. We don't restart our server that often, so the files just build up and stick around for a while.

I know I can create (or find) an application that watches the directory on the server and deletes file that are more than....say....20 minutes old, but I would like to know if anyone knows how to delete the files sooner.

Like, is there a way to delete the files when garbage collection runs or when the JSP session is closed?

Thanks Neowinians, you all rock! :)

Link to comment
https://www.neowin.net/forum/topic/474685-java-delete-temporary-files-when/
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Never mind, I'm a moron....

javax.servlet.http.HttpSessionListener

Using the sessionCreated method I will create folder in the temp directory with the sessions ID name.

Using the sessionDestroyed method I will delete the folder and it's contents.

DUH!

Thanks guys :woot:

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Darwin Mach Kernel/ BSD. Which is both open source.
    • You mean BSD. The UNIX kernel is not open source.
    • I have been on Linux Mint full-time since Jan 2019. while there is a learning curve, it's worth it if you can make the switch, which I can. I mainly browse web, play some games (generally Lutris(Windows games), or through emulators (MAME/Mesen/Flycast etc) etc), and use a small amount of Windows programs (Foobar2000/7-zip/WinRAR/ImgBurn etc) etc. it's nice to dodge the bloat/BS of Windows as you can really feel Linux is all around faster, especially certain things. p.s. recently I enabled NTSync for games since Linux Mint recently offered the 6.14 kernel, which has NTSync support (it's not enabled by default though but you can enable it temporarily for your current boot of the OS through 'sudo modprobe ntsync')
    • A rotten article full of handwaving of anti-consumer practices. Aside from the fact that no, not even close to every piece of major software collects information about its users, ethical developers make such telemetry opt-in and allow it to be completely disabled. To use KDE Plasma as an example, you're shown a greeter upon first boot that gives users the option to send the developers telemetry, with the default being off and 'off' actually meaning off. Windows 10 has never offered that capability - only a promise that Microsoft will slurp up less of your data if you spend time tweaking 50 different privacy settings. There is still absolutely no way to completely opt out of sending Microsoft telemetry in any version of Windows 10 (or 11, naturally). Even using group policy in Enterprise editions only allows you to reduce telemetry to the bare minimum. Home-focused editions don't even get that option. Articles like this dismissing user privacy concerns as "FUD" are a part of the reason Microsoft felt confident enough to go so much further with Windows 11. I guess you get what you deserve in that respect. Personally, I finally made the move to Linux after 15 years or so of dabbling with it, but never really considering a permanent switch. Enjoy your bright, shiny Windows future. You asked for it, after all.
  • Recent Achievements

    • First Post
      smileyhead earned a badge
      First Post
    • One Month Later
      K V earned a badge
      One Month Later
    • Week One Done
      K V earned a badge
      Week One Done
    • Dedicated
      CarlosABC earned a badge
      Dedicated
    • One Month Later
      solidox earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      640
    2. 2
      ATLien_0
      240
    3. 3
      Xenon
      172
    4. 4
      neufuse
      155
    5. 5
      +FloatingFatMan
      123
  • Tell a friend

    Love Neowin? Tell a friend!