Aaron
Aaron

Reputation: 1016

Practical Use to Temp Files

What would be a practical use for temporary files (see code below)?

File temp = File.createTempFile("temp-file-name", ".tmp");

Why can't you store the data you would keep in the file in some variables? If the file is (probably) going to be deleted on the program exit (as "temp" implies), why even create them?

An example can be such as when downloading a file, it often appears as a temporary file while the downloading completes.

Upvotes: 2

Views: 560

Answers (3)

Jeroen Ingelbrecht
Jeroen Ingelbrecht

Reputation: 808

For our little 'imagefilesystem' project (http://code.google.com/p/imagefilesystem/) we actually use the /tmp directory to store the thumbnails we created based upon the images in the local filesystem. So the thumbs were created 'on demand' and were, as the name of /tmp says it itself' temporary of nature so that it didn't create GBs of permanent data.

Upvotes: 1

zevra0
zevra0

Reputation: 209

Aside from the ram versus disk comment above. You may use temp files as precusor files or files about to be processed or served. For example, a server may generate a large PDF for a browser. That PDF file would be stored as a temp file while the (possibly slow) browser downloads the file. Once the communication is complete, the temp file can be destroyed.

Upvotes: 2

AdamSpurgin
AdamSpurgin

Reputation: 961

The two reasons I know of:

  1. As storage space for large chunks of memory you don't need at the moment, when doing memory-intensive tasks like video editing

  2. A kind of hacky way of interproccess communication

Upvotes: 3

Related Questions