About creating files in Java, and writing content

Source: Internet
Author: User

The following content is entirely my original, if reproduced, please specify from: http://www.cnblogs.com/XiOrang/

The first two days in the project because to get a large JSON data via HTTP request (around 300KB) and save, reasoning, and finally decided to get the JSON data to be saved as a file, each time you use to read the file.

Nonsense not much to say, directly on the code.

The following is the code, and the finished code file is available for download at the end of the article.

To create a file method:

Write file Content method:

Delete File Method:

Test:

About file creation, write content, delete. Can be modified according to their own circumstances.

The following is the code class.

 PackageCom.file.run;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.util.UUID;/** * @authorXI Orange-o*@versionJuly 8, 2016 morning 10:38:49*/ Public classForfile {//generate file path    Private StaticString Path = "d:\\file\\"; //file path + name    Private StaticString filenametemp; /*** Create file *@paramfileName file name *@paramfilecontent File Contents *@returnIf the creation succeeds, the success returns True*/     Public Static BooleancreateFile (String filename,string filecontent) {Boolean bool=false; Filenametemp= path+filename+ ". txt";//file path + name + file typeFile File =NewFile (filenametemp); Try {            //If the file does not exist, create a new file            if(!file.exists ())                {File.createnewfile (); BOOL=true; System.out.println ("Success Create File,the file is" +filenametemp); //after the file is created, write the contents to the file.writefilecontent (filenametemp, filecontent); }        } Catch(Exception e) {e.printstacktrace (); }                returnbool; }        /*** Write content to file *@paramfilepath file path and name *@paramwhat Newstr writes to *@return     * @throwsIOException*/     Public Static BooleanWritefilecontent (String filepath,string newstr)throwsioexception{Boolean BOOL=false; String Filein= newstr+ "\ r \ n";//newly-written lines, line breaksString temp = ""; FileInputStream FIS=NULL; InputStreamReader ISR=NULL; BufferedReader BR=NULL; FileOutputStream Fos=NULL; PrintWriter PW=NULL; Try{File File=NewFile (filepath);//file path (including file name)//read the file into the input streamFIS =Newfileinputstream (file); ISR=NewInputStreamReader (FIS); BR=NewBufferedReader (ISR); StringBuffer Buffer=NewStringBuffer (); //Original Contents of the file             for(inti=0; (temp =br.readline ())! =NULL; i++) {buffer.append (temp); //The delimiter between rows and rows is equivalent to "\ n"Buffer = Buffer.append (System.getproperty ("Line.separator"));                        } buffer.append (Filein); FOS=Newfileoutputstream (file); PW=Newprintwriter (FOS);            Pw.write (Buffer.tostring (). ToCharArray ());            Pw.flush (); BOOL=true; } Catch(Exception e) {//Todo:handle ExceptionE.printstacktrace (); }finally {            //don't forget to close            if(PW! =NULL) {pw.close (); }            if(Fos! =NULL) {fos.close (); }            if(BR! =NULL) {br.close (); }            if(ISR! =NULL) {isr.close (); }            if(FIS! =NULL) {fis.close (); }        }        returnbool; }        /*** Delete Files *@paramfileName file name *@return     */     Public Static Booleandelfile (String fileName) {Boolean bool=false; Filenametemp= path+filename+ ". txt"; File File=NewFile (filenametemp); Try {            if(File.exists ()) {file.delete (); BOOL=true; }        } Catch(Exception e) {//Todo:handle Exception        }        returnbool; }     Public Static voidMain (string[] args) {uuid uuid=Uuid.randomuuid (); CreateFile (UUID+ "MyFile", "my dream said don't stay waiting, let the light refraction tears of the pupil, reflected in the heart most want to have rainbow, take me to that piece has your sky, because you are my dream my dream"); }                                                                }

About creating files in Java, and writing content

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.