好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java使用文件流实现查看下载次数

本文实例为大家分享了 java 使用 文件流 实现查看 下载次数 的具体代码,供大家参考,具体内容如下

需求:点击一个按钮的次数或者是展示文件,游戏被下载的次数

实现:开辟一个流文件,用来保存被下载的次数,然后读文件中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 {

       /** 读取文件中的内容 */

       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++) {

         // 值字节在0-255 范围之内是作为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]);  // 一个字节一个字节的写入

       }

       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();

     }

 

   } 

 

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://blog.csdn.net/xusheng_mr/article/details/78801506

查看更多关于java使用文件流实现查看下载次数的详细内容...

  阅读:58次