Create a temporary file in Java

Source: Internet
Author: User

 

Createtempfile(String prefix, string suffix)
Create an empty file in the default temporary file directory and use the given prefix and suffix to generate its name.

Createtempfile(String prefix, string suffix, file directory)
Create a new empty file in the specified directory and generate its name using the given prefix and suffix string.

Public voidDeleteonexit();

When the virtual machine is terminated, a request is sent to delete the files or directories represented by this abstract path name. Files (or directories) will be deleted in the reverse order of registration. It is invalid to call this method to delete a file or directory registered as deleted. According to the definition in the Java language specification, the deletion operation is attempted only when the virtual machine is terminated normally.

Once a delete operation is requested, the request cannot be canceled. Therefore, use this method with caution.

Package COM. test; import Java. io. file; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. ioexception; public class test {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stub int B, C; byte [] buffer = new byte [100]; file F = new file ("C: \ test"); file ftemp = NULL; try {ftemp = file. createtempfile ("letter ",". TXT ", f); syste M. out. println ("enter a line of text:"); B = system. in. read (buffer); fileoutputstream writefile = new fileoutputstream (ftemp); writefile. write (buffer, 0, B); fileinputstream in = new fileinputstream (ftemp); fileoutputstream out = new fileoutputstream ("C: \ testline.txt"); While (C = in. read ())! =-1) {out. Write (c);} system. Out. println ("input complete !! "); Writefile. close (); In. close (); out. close ();} catch (ioexception E1) {e1.printstacktrace ();} finally {ftemp. deleteonexit ();}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.