Sunday 13, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Thursday, 14 May 2015

How to create temp file using Java

Following example shows how to create temp file using file.createTempFile() method in Java.
createTempFile
public static File createTempFile(String prefix,String suffix) throws IOException Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name. Invoking this method is equivalent to invoking createTempFile(prefix, suffix, null).
The Files.createTempFile method provides an alternative method to create an empty file in the temporary-file directory. Files created by that method may have more restrictive access permissions to files created by this method and so may be more suited to security-sensitive applications.
Parameters:
prefix - The prefix string to be used in generating the file's name; must be at least three characters long
suffix - The suffix string to be used in generating the file's name; may be null, in which case the suffix ".tmp" will be used
Returns:
An abstract pathname denoting a newly-created empty file
Throws:
IllegalArgumentException - If the prefix argument contains fewer than three characters
IOException - If a file could not be created
SecurityException - If a security manager exists and its
SecurityManager.checkWrite(java.lang.String) method does not allow a file to be created
Sample Program
CreateTempFile.java
01.package com.javatutorialcorner.file;
02. 
03.import java.io.BufferedWriter;
04.import java.io.File;
05.import java.io.FileWriter;
06.import java.io.IOException;
07. 
08.public class CreateTempFile {
09. 
10. public static void main(String[] args) throws IOException {
11.  File tempFile = File.createTempFile("tempfile", ".txt");
12.  tempFile.deleteOnExit();
13.  BufferedWriter out = new BufferedWriter(new FileWriter(tempFile));
14.  out.write("test temp file ");
15.  System.out.println("temporary file tempfile.txt created:");
16.  out.close();
17. 
18. }
19. 
20.}

Result
The above code will create temp file and produce the following output.

temporary file tempfile.txt created

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • How to find parent directory of file using JavaString java.io.File.getParent()Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory. The parent of an abstract pathname consists of the pathname's prefix, i… Read More
  • How to delete directory using Java boolean java.io.File.delete() Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted. Note that the java.nio.file.Fil… Read More
  • How to traverse the directories using Java This program explains how to traverse the directories and sub directories using Java DirectoryTraversal.java package com.javatutorialcorner.directory; import java.io.File; public class DirectoryTraversal { public s… Read More
  • How to find the size of directory using FileUtils public static long sizeOfDirectory(File directory) Counts the size of a directory recursively (sum of the length of all files). Note that overflow is not detected, and the return value may be negative if overflow occurs. See… Read More
  • How to check directory or file is hidden using Java boolean java.io.File.isHidden() Tests whether the file named by this abstract pathname is a hidden file. The exact definition of hidden is system-dependent. On UNIX systems, a file is considered to be hidden if its name beg… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to create temp file using Java Rating: 5 Reviewed By: eHowToNow