Skip to main content

Temporary files and directories in Java 7 and before

Sometimes we want to create a temporary file, whether to save some data that gets written by some other application or just to temporary store stuff. Well, usually applications have their own temporary folder where they do this and it gets somehow configured. But why not use the underlying OS specific file like "/tmp/" in Linux so there must be some system property that has this info and there is. The key is "" resulting in "/tmp" in my case or by code:

String tempDir = System.getProperty("");

We can use tempDir  folder as a temporary place to store files, but there are a lot nicer ways to work with files like this even in JDK6 not just in JDK7:

import java.nio.file.Files;
import java.nio.file.Path;

public class TempFile {

   public static void main(String[] args) {

    try {
      // create a temp file
      File tempFile = File.createTempFile("old-file", ".tmp");
      System.out.println("Temp file : " + tempFile.getAbsolutePath());
      // nio style
      final Path path = Files.createTempFile("nio-temp", ".tmp");
      System.out.println("Temp file : " + path);
      //call the same delete on exit

      Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
          try {
            System.out.println("deleted file at "+path);
          } catch (IOException e) {

    } catch (IOException e) {




The JDK 7 way is the use the Files class and use Path 's that can easily enable us way to create and work files and their attributes and permissions. Calling tempFile.deleteOnExit() or path.toFile().deleteOnExit() will enable the files to be deleted automatically after the virtual machine terminates. Files are deleted in the reverse order that they are registered and besides that we need to be careful about what the path is.
Deletion will only happen if JVM is closed under normal condition and not for example using kill -9 (SIGKILL) under Linux.

One other interesting option is to do the deletion with a shutdown hook using "Runtime.getRuntime().addShutdownHook(new Thread() {...}" that registers  a new virtual-machine shutdown hook. This shutdown hook is simply an initialized thread, but it is not started. The thread will only be executed when the virtual machine begins its shutdown sequence during normal shutdown.

Related Links

Popular posts from this blog

HTML 5 data-* attributes, how to use them and why

It is always tempting to add custom attributes in HTML so that you can use the data stored there to do X. But if you do that there is no way of knowing if your HTML attribute will not be overridden in the future and used for something else and additionally you will not be writing valid HTML markup that can pass HTML 5 validator and with that you can create some very bad side effects. That is why there is a spec in HTML 5 called custom data attributes that enable number of useful features.

You may go around and read the specs, but the basic idea is very simple, you can add any attribute that starts with "data-" and that attribute will be treated as non-visible data for that attribute. By non-visible I mean that it is not something that gets rendered to the client so it does not affect the layout or style of the page, but it is there in the HTML so in no way this is private.
So let's get right into it, the following snippet is a valid HTML5 markup

<div id="aweso…

Basic Authentication with RestTemplate

Spring Rest Templates are very good way of writing REST clients. By default they work with basic HTTP so if we need to use Basic Authorization we would need to init the rest template with custom HttpClient. This way the Rest Template will automatically use Basic Auth and append to the HTTP headers "Authorization: Basic BASE64ENCODED_USER_PASS".

HttpClient client = new HttpClient(); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("USERNAME","PASS"); client.getState().setCredentials( new AuthScope("", 9090, AuthScope.ANY_REALM), credentials); CommonsClientHttpRequestFactory commons = new CommonsClientHttpRequestFactory(client); RestTemplate template = new RestTemplate(commons); SomeObject result = template.getForObject( "",SomeObject.class );

In EE application this would probably be managed by DI framework like Spring Core and only initialized once sin…

How to Not Hate JavaScript: Tips from the Frontline

This article was originally published on voxxed under,

In my work assisting teams with JavaScript related problems, I’ve noticed some common issues. If you're experiencing frustrations with the language too, this article might be of some assistance. Disclaimer: a few of my tips might be obvious to some of you, but hopefully you’ll find at least some useful nuggets of information here! These pointers are especially useful when dealing with enterprise applications and CMS solutions. This is where we have our code, the CMS code, the code from that team nobody wants to mention...and, of course, all of them are loaded asynchronously.
The Debugger Statement
This is one of the most underused features when dealing with JavaScript, especially since it’s supported by the majority of browsers. The debugger statement was part of the first edition of ECMA Script so it's been here for quite some time.