Java temporary file read/write

Source: Internet
Author: User
static File                  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.
static File               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.
void                    deleteOnExit()
When the virtual machine is terminated, a request is sent to delete the files or directories represented by this abstract path name.

 

Try {<br/> // obtain the current path of the Program <br/> string strdir = system. getproperty ("user. dir "); <br/> // change the path separator <br/> string folderpath = strdir. replace ('//', file. separatorchar); <br/> string filepath = folderpath + file. separatorchar + "josp. TMP "; <br/> file = new file (folderpath); <br/> // create Temp File <br/> file temp = file. createtempfile ("josp ",". TMP ", file); </P> <p> // Delete temp file when program exits. <br /> Temp. deleteonexit (); </P> <p> // write to file. True indicates that write continues at the end, false indicates that the original content is overwritten. <br/> bufferedwriter out = new bufferedwriter (New filewriter (temp, true); <br/> out. write ("astring/N"); <br/> out. write ("aaaaaaaaa/N"); <br/> out. close (); <br/> out = new bufferedwriter (New filewriter (temp, true); <br/> out. write ("astring/N"); <br/> out. write ("aaaaaaaaa/N"); <br/> out. close (); </P> <p> bufferedreader in = new Bufferedreader (New filereader (temp); <br/> string STR; <br/> while (STR = in. Readline ())! = NULL) <br/> system. out. println (STR); <br/> in. close (); </P> <p >}catch (ioexception e) {<br/>}

Related Article

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.