Use of "Java" file: Writes a string out to a local file, the reason for size 0kb

Source: Internet
Author: User

Implementation method:

Temporarily write a method that writes the string out to a local file , which can be supplemented in more ways later:

  Public Static voidMain (string[] args) {/*** ============== to prepare some data-start===============*/String FileName=Uuid.randomuuid (). toString (); String FilePath= "E:/sxcontext" + "/" +FileName; String content= ""; StringBuilder StringBuilder=NewStringBuilder (); intA = 10;  for(inti = 0; i < A; i++) {stringbuilder.append (Uuid.randomuuid (). toString ()+ "\ n"); } content= "Test text file: \ r \ n" +stringbuilder.tostring (); /*** ============== to prepare some data-end===============*/        /*** ===============start-writes a string to a local file =================*/File File=NewFile (FilePath); //newly created if the parent file does not existFile Parentfile =File.getparentfile (); if(!parentfile.exists ())        {Parentfile.mkdirs (); } FileWriter FW=NULL; BufferedWriter BW=NULL; Try {            if(!file.exists ())            {File.createnewfile (); } FW=NewFileWriter (file); Bw=NewBufferedWriter (FW);             Bw.write (content);        Bw.flush (); } Catch(IOException e) {e.printstacktrace (); }finally {            Try{bw.close ();            Fw.close (); } Catch(IOException e) {e.printstacktrace (); }        }        /*** ===============end-writes a string to a local file =================*/    }
View Code

Note

Bw.write (content);

Can be executed multiple times in the FOR loop!!!

If you are concerned, you can finish the Bw.write ()

Perform:

Bw.flush ();

=======================

As for writing the text to a local file, whether or not the local file you write is not a TXT file or a file without a suffix, as long as

1. Parent directory does not exist, create parent directory

2.file file judge exist () if not createnewfile ()

3. Write the text to the local

4.flush ()

5.close ()

There may be several reasons for the local file 0kb size:

1.fileName is the file name to be created contains illegal characters, the most common illegal character is: colon

2. Either the close () normal shutdown flow is not performed

3.flush () method can not be executed

Use of "Java" file: Writes a string out to a local file, the reason for size 0kb

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.