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