国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java創建文件且寫入內容的方法

2019-11-26 14:05:07
字體:
來源:轉載
供稿:網友

前兩天在項目中因為要通過http請求獲取一個比較大的json數據(300KB左右)并且保存,思來想去,最后還是決定將獲取到的json數據以文件的形式保存下來,每次使用的時候去讀取文件就可以了。

廢話不多說了,直接上代碼。

以下是代碼截圖,文章結尾會有完成的代碼文件可供下載。

創建文件方法:

寫入文件內容方法:

刪除文件方法:

測試:

關于文件創建,寫入內容,刪除??梢愿鶕约旱那闆r再稍作修改。

以下是代碼類。

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;/*** 創建文件* @param fileName 文件名稱* @param filecontent 文件內容* @return 是否創建成功,成功則返回true*/public static boolean createFile(String fileName,String filecontent){Boolean bool = false;filenameTemp = path+fileName+".txt";//文件路徑+名稱+文件類型File file = new File(filenameTemp);try {//如果文件不存在,則創建新的文件if(!file.exists()){file.createNewFile();bool = true;System.out.println("success create file,the file is "+filenameTemp);//創建文件成功后,寫入內容到文件里writeFileContent(filenameTemp, filecontent);}} catch (Exception e) {e.printStackTrace();}return bool;}/*** 向文件中寫入內容* @param filepath 文件路徑與名稱* @param newstr 寫入的內容* @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();//文件原有內容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 exceptione.printStackTrace();}finally {//不要忘記關閉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創建文件且寫入內容的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 卫辉市| 陇南市| 舞阳县| 象山县| 门源| 延长县| 义乌市| 晋江市| 东安县| 大邑县| 清水河县| 高邮市| 清水县| 文登市| 马公市| 尼勒克县| 木兰县| 西乡县| 陆良县| 兴海县| 扎鲁特旗| 东乡族自治县| 内丘县| 于田县| 宕昌县| 麦盖提县| 东源县| 监利县| 威远县| 濮阳市| 清河县| 九龙县| 元阳县| 彰化市| 麻栗坡县| 武川县| 山西省| 东丽区| 广东省| 武平县| 宁夏|