When files are being processed in an application, they are typically held in a temporary location. There are times when a developer needs to know the location of these temp files, and, fortunately, Java has a method we can use to learn this location. For example, you may need to clean up these temporary files at a later date or reference them further in the code.
Here is how you locate temp files in Java:
public class TemporaryLocation { public static void main(String args[]) { TemporaryLocation temporaryLocation = new TemporaryLocation(); temporaryLocation.proceed(); } private void proceed() { String tempLocation = System.getenv("TEMP"); System.out.println("TEMP location in your system is " + tempLocation); } } /*
Running this code will produce the following output – or something similar, depending upon your computer’s settings and configuration:
[root@mypc]# java TemporaryLocation TEMP location in your system is /var/tmp/