標籤:filename ade oid throw 檔案中 throws toolbar title http
以下內容完全為本人原創,如若轉載,請註明出自:http://www.cnblogs.com/XiOrang/
前兩天在項目中因為要通過http請求擷取一個比較大的json資料(300KB左右)並且儲存,思來想去,最後還是決定將擷取到的json資料以檔案的形式儲存下來,每次使用的時候去讀取檔案就可以了。
廢話不多說了,直接上代碼。
以下是代碼,文章結尾會有完成的代碼檔案可供下載。
建立檔案方法:
寫入檔案內容方法:
刪除檔案方法:
測試:
關於檔案建立,寫入內容,刪除。可以根據自己的情況再稍作修改。
以下是代碼類。
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 exception e.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中建立檔案,並且寫入內容