wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes File API createTempFile(..) method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "File API createTempFile(..) method" Watch "File API createTempFile(..) method" New topic
Author

File API createTempFile(..) method

kirba devi
Ranch Hand

Joined: Jun 29, 2007
Posts: 50
Hi
i have written a java file to test methos in File API.
import java.io.*;


public class TestFile {
// static final transient int maxElements = 100;

public static void main(String[] args) {

System.out.println(System.getProperty("user.dir"));

File f=new File("D:\\ABC.java");
System.out.println(f.exists());
System.out.println(File.pathSeparator);
System.out.println(File.pathSeparatorChar);
System.out.println(File.separator);
System.out.println(File.separatorChar);
System.out.println(f.canRead());
System.out.println(f.canWrite());
//System.out.println(f.delete());
System.out.println(f.exists());
try{
System.out.println(f.createNewFile());
System.out.println(f.getAbsolutePath());
System.out.println(f.getCanonicalPath());
}catch(IOException ioe){
ioe.printStackTrace();
}
System.out.println(f.isHidden());
System.out.println(f.isDirectory());
System.out.println(f.isFile());
System.out.println(f.isAbsolute());
System.out.println(f.getParent());
System.out.println(f.getPath());
System.out.println(f.getName());
f.deleteOnExit();
try {
File f1=File.createTempFile("ftg", null, new File("D:\\") );//Line1
}catch(IOException ioe){
ioe.printStackTrace();
}

Output:ftg43785.tmp.How?
Expected output:ftg.tmp.

Please help

Regards
Kirba




}

}
kirba devi
Ranch Hand

Joined: Jun 29, 2007
Posts: 50
Also for each time when i run the file it is showing different numbers..

ftg34545.tmp

Regards
Kirba
Pramod Yadav
Greenhorn

Joined: Apr 02, 2008
Posts: 1
Please read the JDK Doc for File API.
You will come to know.

createTempFile
public static File createTempFile(String prefix,
String suffix,
File directory)
throws IOExceptionCreates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name. If this method returns successfully then it is guaranteed that:

The file denoted by the returned abstract pathname did not exist before this method was invoked, and
Neither this method nor any of its variants will return the same abstract pathname again in the current invocation of the virtual machine.
This method provides only part of a temporary-file facility. To arrange for a file created by this method to be deleted automatically, use the deleteOnExit() method.
The prefix argument must be at least three characters long. It is recommended that the prefix be a short, meaningful string such as "hjb" or "mail". The suffix argument may be null, in which case the suffix ".tmp" will be used.

To create the new file, the prefix and the suffix may first be adjusted to fit the limitations of the underlying platform. If the prefix is too long then it will be truncated, but its first three characters will always be preserved. If the suffix is too long then it too will be truncated, but if it begins with a period character ('.') then the period and the first three characters following it will always be preserved. Once these adjustments have been made the name of the new file will be generated by concatenating the prefix, five or more internally-generated characters, and the suffix.

If the directory argument is null then the system-dependent default temporary-file directory will be used. The default temporary-file directory is specified by the system property java.io.tmpdir. On UNIX systems the default value of this property is typically "/tmp" or "/var/tmp"; on Microsoft Windows systems it is typically "C:\\WINNT\\TEMP". A different value may be given to this system property when the Java virtual machine is invoked, but programmatic changes to this property are not guaranteed to have any effect upon the temporary directory used by this method.


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
directory - The directory in which the file is to be created, or null if the default temporary-file directory is to 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
Since:
1.2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File API createTempFile(..) method
 
Similar Threads
De-Serializing Objects
compile at run time????
array
Exceptions in Runtime
Need help with Exceptions