Two days ago, I had to obtain a relatively large json data (about 300KB) and save it through http request. After thinking about it, I finally decided to save the obtained json data in the form of a file and read the file every time I use it.
Without further ado, just upload the code.
The following is a screenshot of the code. There will be a completed code file available for download at the end of the article.
How to create a file:
How to write file content:
How to delete a file:
test:
About file creation, write content, delete. You can make some changes according to your own situation.
The following is the 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 Xijuzi-O* @version July 8, 2016 at 10:38:49 am */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 is successful, it will return true*/public static boolean createFile(String fileName,String filecontent){Boolean bool = false;filenameTemp = path+fileName+".txt";//File path+name+file type File 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 creating the file, writeFileContent(filenameTemp, filecontent);}} catch (Exception e) {e.printStackTrace();}return bool;}/*** Write content to the file* @param filepath File path and name* @param newsr Content written* @return* @throws IOException*/public static boolean writeFileContent(String filepath,String newsstr) throws IOException{Boolean bool = false;String filein = newsstr+"/r/n";//Newly written 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);//File path (including file name)//Read the file into the input stream fis = new FileInputStream(file); isr = new InputStreamReader(fis);br = new BufferedReader(isr);StringBuffer buffer = new StringBuffer();//The original content of the file for(int i=0;(temp =br.readLine())!=null;i++){buffer.append(temp);//The separator between lines 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 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();}}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(uuid+"myfile", "My dream says don't stay and wait, let the light refract the tears wet pupils, reflect the rainbow that I want to have the most, and take me to the sky with you, because you are my dream, my dream");}}The above is the method of creating files and writing content in Java introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!