Java創(chuàng)建文件且寫入內(nèi)容的方法
前兩天在項目中因為要通過http請求獲取一個比較大的json數(shù)據(jù)(300KB左右)并且保存,思來想去,最后還是決定將獲取到的json數(shù)據(jù)以文件的形式保存下來,每次使用的時候去讀取文件就可以了。
廢話不多說了,直接上代碼。
以下是代碼截圖,文章結(jié)尾會有完成的代碼文件可供下載。
創(chuàng)建文件方法:
寫入文件內(nèi)容方法:
刪除文件方法:
測試:
關(guān)于文件創(chuàng)建,寫入內(nèi)容,刪除??梢愿鶕?jù)自己的情況再稍作修改。
以下是代碼類。
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 夕橘子-O * @version 2016年7月8日 上午10:38:49 */ public class ForFile { //生成文件路徑 private static String path = "D:\\file\\"; //文件路徑+名稱 private static String filenameTemp; /** * 創(chuàng)建文件 * @param fileName 文件名稱 * @param filecontent 文件內(nèi)容 * @return 是否創(chuàng)建成功,成功則返回true */ public static boolean createFile(String fileName,String filecontent){ Boolean bool = false; filenameTemp = path+fileName+".txt";//文件路徑+名稱+文件類型 File file = new File(filenameTemp); try { //如果文件不存在,則創(chuàng)建新的文件 if(!file.exists()){ file.createNewFile(); bool = true; System.out.println("success create file,the file is "+filenameTemp); //創(chuàng)建文件成功后,寫入內(nèi)容到文件里 writeFileContent(filenameTemp, filecontent); } } catch (Exception e) { e.printStackTrace(); } return bool; } /** * 向文件中寫入內(nèi)容 * @param filepath 文件路徑與名稱 * @param newstr 寫入的內(nèi)容 * @return * @throws IOException */ public static boolean writeFileContent(String filepath,String newstr) throws IOException{ Boolean bool = false; String filein = newstr+"\r\n";//新寫入的行,換行 String temp = ""; FileInputStream fis = null; InputStreamReader isr = null; BufferedReader br = null; FileOutputStream fos = null; PrintWriter pw = null; try { File file = new File(filepath);//文件路徑(包括文件名稱) //將文件讀入輸入流 fis = new FileInputStream(file); isr = new InputStreamReader(fis); br = new BufferedReader(isr); StringBuffer buffer = new StringBuffer(); //文件原有內(nèi)容 for(int i=0;(temp =br.readLine())!=null;i++){ buffer.append(temp); // 行與行之間的分隔符 相當于“\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 { //不要忘記關(guān)閉 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; } /** * 刪除文件 * @param fileName 文件名稱 * @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", "我的夢說別停留等待,就讓光芒折射淚濕的瞳孔,映出心中最想擁有的彩虹,帶我奔向那片有你的天空,因為你是我的夢 我的夢"); } }
以上所述是小編給大家介紹的Java創(chuàng)建文件且寫入內(nèi)容的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
springMvc注解之@ResponseBody和@RequestBody詳解
本篇文章主要介紹了springMvc注解之@ResponseBody和@RequestBody詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05Java程序員編程性能優(yōu)化必備的34個小技巧(總結(jié))
這篇文章主要介紹了Java程序員編程性能優(yōu)化必備的34個小技巧(總結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-07-07SpringBoot通過自定義注解實現(xiàn)參數(shù)校驗
實現(xiàn)參數(shù)校驗說實話方式還挺多,個人使用過直接在Controller代碼里面寫、AOP+自定義注解、ConstraintValidator。本文主要和大家講的是ConstraintValidator實現(xiàn),感興趣的可以了解一下2022-12-12Hbase、elasticsearch整合中jar包沖突的問題解決
本篇文章主要介紹了Hbase、elasticsearch整合中jar包沖突的問題解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12