Create temporary file in specified directory

  1. /*
  2.   Create temporary file in specified directory
  3.   This Java example shows how to create a new temporary file at specified path using
  4.   createTempFile method of Java File class.
  5. */
  6.  
  7. import java.io.*;
  8.  
  9. public class CreateTempFileDirectory {
  10.  
  11. public static void main(String[] args) {
  12.  
  13. /*
  14.   * To create temporary file at specified location use,
  15.   * static File createTempFile(String namePrefix, String nameSuffix, File dir) method
  16.   * of Java File class,
  17.   *
  18.   * where namePrefix is a prefix string used to generate
  19.   * a file's name and must be atleast 3 characters long and nameSuffix is a
  20.   * suffix string used to generate suffix of the temporary file name, may be null,
  21.   * and in that case default ".tmp" will be used as a suffix. dir is the
  22.   * directory under which the temporary file will be created.
  23.   */
  24.  
  25. File file = null;
  26. File dir = new File("C://FileIO");
  27.  
  28. try
  29. {
  30. file = File.createTempFile("JavaTemp", ".javatemp", dir);
  31. }
  32. catch(IOException ioe)
  33. {
  34. System.out.println("Exception creating temporary file : " + ioe);
  35. }
  36.  
  37. /*
  38.   * Please note that if the directory does not exists, IOException will be
  39.   * thrown and temporary file will not be created.
  40.   */
  41. System.out.println("Temporary file created at : " + file.getPath());
  42. }
  43. }
  44.  
  45. /*
  46. Typical output would be
  47. Temporary file created at : C:\FileIO\JavaTemp40534.javatemp
  48. */

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine




Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!