前兩天在項目中因為要通過http請求獲取一個比較大的json數據(300KB左右)并且保存,思來想去,最后還是決定將獲取到的json數據以文件的形式保存下來,每次使用的時候去讀取文件就可以了。
廢話不多說了,直接上代碼。
以下是代碼截圖,文章結尾會有完成的代碼文件可供下載。
創建文件方法:
寫入文件內容方法:
刪除文件方法:
測試:
關于文件創建,寫入內容,刪除。可以根據自己的情況再稍作修改。
以下是代碼類。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
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創建文件且寫入內容的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://www.cnblogs.com/XiOrang/p/5652875.html