Create temporary file

  1. /*
  2.   Create temporary file
  3.   This Java example shows how to create a new temporary file using
  4.   createTempFile method of Java File class.
  5. */
  6.  
  7. import java.io.*;
  8.  
  9. public class CreateTemporaryFile {
  10.  
  11. public static void main(String[] args) {
  12.  
  13. /*
  14.   * To create temporary file use,
  15.   * static File createTempFile(String namePrefix, String nameSuffix) 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. Suffix
  21.   * may be null, and in that case default ".tmp" will be used as a suffix.
  22.   */
  23.  
  24. File file1 = null;
  25. File file2 = null;
  26.  
  27. try
  28. {
  29. //create temporary file wihtout extension suffix
  30. file1 = File.createTempFile("JavaTemp", null);
  31.  
  32. //create temporary file with specified extension suffix
  33. file2 = File.createTempFile("JavaTemp", ".javatemp");
  34.  
  35. }
  36. catch(IOException ioe)
  37. {
  38. System.out.println("Exception creating temporary file : " + ioe);
  39. }
  40.  
  41. /*
  42.   * Temporary file will be created in default temporary directory of operating
  43.   * system.
  44.   */
  45. System.out.println("Temporary file without suffix extension: "
  46. + file1.getPath());
  47. System.out.println("Temporary file with suffix extension: "
  48. + file2.getPath());
  49.  
  50.  
  51. }
  52. }
  53.  
  54. /*
  55. Typical output would be
  56. Temporary file without suffix extension: C:\Temp\JavaTemp45096.tmp
  57. Temporary file with suffix extension: C:\Temp\JavaTemp45097.javatemp
  58. */

Post new comment

To combat spam, please enter the code in the image.


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!