The first two days in the project to get a larger JSON data (around 300KB) via HTTP requests and save, reasoning, finally decided to save the captured JSON data in the form of a file, and read the file every time you use it.
Nonsense not much said, directly on the code.
The following is a screenshot of the code that will have the completed code file available for download at the end of the article.
To create a file method:
Write file Content method:
To delete a file method:
Test:
About file creation, write content, delete. can be slightly modified according to their own situation.
The following is a code class.
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 XI Orange-O * @version July 8, 2016 morning 10:38:49/public class Forfile {//Generate file path private static String path = "D:\
\file\\ ";
File path + name private static String filenametemp; /** * Create file * @param filename File name * @param filecontent file content * @return Whether the creation succeeds, SUCCESS returns TRUE * */public static Boolean CreateFile (String filename,string filecontent) {Boolean bool = false; Filenametemp = path+filename+ ". txt";//file path + name + file-type file = new filename (filenametemp); try {//If text
If the piece 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 ();} return bool; /** * Write content to file * @paramFilePath file path and name * @param newstr Write content * @return * @throws IOException/public static Boolean writefilecontent (String fi
Lepath,string newstr) throws ioexception{Boolean bool = false;
String filein = newstr+ "\ r \ n";//new Write line, newline string temp = "";
FileInputStream FIS = null;
InputStreamReader ISR = null;
BufferedReader br = null;
FileOutputStream fos = null;
PrintWriter pw = null; try {File File = new file (filepath);//filename path (including file name)//read the file into the input stream FIS = new FileInputStream (file); ISR = new Inputstreamreade
R (FIS);
br = new BufferedReader (ISR);
StringBuffer buffer = new StringBuffer (); The original contents of the file for (int i=0; (temp =br.readline ())!=null;i++) {buffer.append (temp);//The separator between rows and rows corresponds 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 {//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 ();}
return bool; /** * Delete file * @param filename File name * @return/public static Boolean delfile (String fileName) {Boolean bool = FALSE; fil
Enametemp = 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 (uuid+ "myfile", "my dream said don't stay wait, let the light refract the tears wet pupil
, reflected in the hearts of the most want to have the rainbow, take me to the piece of your sky, because you are my dream my Dream "); }
}
The above is a small set of Java to introduce the creation of the file and write the content of the method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!