Create and write files and content in java

Source: Internet
Author: User
Tags file type file

Create and write files and content in java

The following content is completely original, if reproduced, please indicate from: http://www.cnblogs.com/XiOrang/

 

Two days ago, in the project, I tried to get a large json data (about KB) and save it by using an http request, finally, we decided to save the obtained json data as a file and read the file each time.

If you don't talk much about it, simply add the code.

The following code is available at the end of the article.

 

File Creation method:

Method for writing file content:

 

File Deletion method:

 

Test:

 

Create, write, and delete files. You can make some modifications as needed.

The following are code classes.

Package com. file. run; import java. io. bufferedReader; import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStreamReader; import java. io. printWriter; import java. util. UUID;/*** @ author evening orange-O * @ version 10:38:49, January 1, July 8, 2016 */public class ForFile {// generate the file path private static String path = "D: \ file \ "; // file path + name private static Strin G filenameTemp;/*** Create File ** @ param fileName file name * @ param filecontent file content * @ return whether the file is successfully created, true */public static boolean createFile (String fileName, String filecontent) {Boolean bool = false; filenameTemp = path + fileName + ". txt "; // File path + name + file type File = new file (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 successfully, write the content to the file writeFileContent (filenameTemp, filecontent );}} catch (Exception e) {e. printStackTrace ();} return bool ;} /*** write content to the file * @ param filepath file path and name * @ param newstr write content * @ return * @ throws IOException */public static boolean writeFileContent (String filepath, String newstr) throws IOException {Boolean bool = false; String filein = newstr + "\ r \ n"; // new row written, line feed String temp = ""; fileInputStream FCM = null; InputStreamReader isr = null; BufferedReader br = null; FileOutputStream fos = null; PrintWriter pw = null; try {File file = new File (filepath ); // file path (including the file name) // read the file into the input stream. The input stream is: new FileInputStream (file); isr = new InputStreamReader (FS); br = new BufferedR Eader (isr); StringBuffer buffer = new StringBuffer (); // original file content for (int I = 0; (temp = br. readLine ())! = Null; I ++) {buffer. append (temp); // The delimiter between the row and the row is equivalent to "\ n" buffer = buffer. append (System. getProperty ("line. separator ");} buffer. append (filein); fos = new FileOutputStream (file); pw = new PrintWriter (fos); pw. write (buffer. toString (). toCharArray (); pw. flush (); bool = true;} catch (Exception e) {// TODO: handle exception e. printStackTrace ();} finally {// do not forget to close if (pw! = Null) {pw. close () ;}if (fos! = Null) {fos. close () ;}if (br! = Null) {br. close () ;}if (isr! = Null) {isr. close () ;}if (FS! = Null) {FS. close () ;}} return bool;}/*** delete file * @ param fileName file name * @ return */public static boolean delFile (String fileName) {Boolean bool = false; filenameTemp = path + fileName + ". txt "; File file = new File (filenameTemp); try {if (file. exists () {file. delete (); bool = true;} catch (Exception e) {// TODO: handle exception} return bool;} public static void main (String [] args) {UUID uuid = UUID. randomUUID (); createFile, take me to your sky, because you are my dream ");}}

 

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.