本文實例為大家分享了java使用文件流實現(xiàn)查看下載次數(shù)的具體代碼,供大家參考,具體內(nèi)容如下
需求:點擊一個按鈕的次數(shù)或者是展示文件,游戲被下載的次數(shù)
實現(xiàn):開辟一個流文件,用來保存被下載的次數(shù),然后讀文件中value,點擊一次value加1,再將此value保存到流文件中。
三種方法:
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
package cn.tr.test; import java.io.bufferedreader; import java.io.bufferedwriter; import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.filereader; import java.io.filewriter; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.io.reader; import java.io.writer; public class testdemo { private static int in ; private static file file; public static void main(string[] args) { fun2(); } public static void fun(){ /** 初始化文件中的值為0*/ try { outputstream out = new fileoutputstream(file); string str = "00" ; out.write(str.getbytes()); out.close(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } public static void fun2() { file= new file( "d:/test/d.txt" ); if (!file.exists()) { try { file.createnewfile(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } try { /** 讀取文件中的內(nèi)容 */ if (file.exists()&&file.length()== 0 ) { fun(); } inputstream is = new fileinputstream(file); byte b[] = new byte [( int ) file.length()]; for ( int i = 0 ; i < b.length; i++) { // 值字節(jié)在0-255 范圍之內(nèi)是作為int 來返回的 b[i] = ( byte ) is.read(); } in =integer.parseint( new string(b)); in++; system.out.println( "讀出來的" +in); /**再寫入到文件中 */ outputstream out = new fileoutputstream(file); string str = string.valueof(in); byte [] bytes = str.getbytes(); for ( int i = 0 ; i < bytes.length; i++) { out.write(bytes[i]); // 一個字節(jié)一個字節(jié)的寫入 } is.close(); out.close(); system.out.println( "寫入的" +in); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } public static void fun3(){ file= new file( "d:/test/d.txt" ); if (!file.exists()) { try { file.createnewfile(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } /** 先讀出來*/ try { if (file.exists()&&file.length()== 0 ) { fun(); } reader reader = new filereader(file); char [] c = new char [( int )file.length()]; int temp = 0 ; int len = 0 ; while ((temp=reader.read()) != - 1 ){ c[len]=( char )temp; len++; } reader.close(); system.out.println( "初始值" + new string(c, 0 ,len)); in =integer.parseint( new string(c, 0 ,len)); in++; system.out.println( "下載一次:" +in); /** 再寫進去*/ writer writer = new filewriter(file); writer.write(in+ "" ); writer.close(); system.out.println( "再寫進去:" +in); } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } public static void fun4(){ reader reader; writer writer; file= new file( "d:/test/d.txt" ); if (!file.exists()) { try { file.createnewfile(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } try { if (file.exists()&&file.length()== 0 ) { fun(); } /** 讀出來*/ reader = new filereader(file); bufferedreader br = new bufferedreader(reader); char [] c = new char [( int )file.length()]; int len = 0 ; int temp = 0 ; while ((temp=br.read())!= - 1 ){ c[len]=( char )temp; len++; } in =integer.parseint( new string(c, 0 , len)); in++; system.out.println( "讀出來:" + in); /** 寫進去*/ writer = new filewriter(file); bufferedwriter bw = new bufferedwriter(writer); bw.write(in+ "" ); system.out.println( "寫進去:" +in); br.close(); bw.close(); } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } } |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/xusheng_mr/article/details/78801506