本文实例为大家分享了java使用文件流实现查看下载次数的具体代码,供大家参考,具体内容如下
需求:点击一个按钮的次数或者是展示文件,游戏被下载的次数
实现:开辟一个流文件,用来保存被下载的次数,然后读文件中值点击一次值加1,再将此值保存到流文件中。
三种方法:
包cn.tr.test; 进口java.io.BufferedReader; 进口java.io.BufferedWriter; 进口java.io.File; 进口java.io.FileInputStream; 进口java.io.FileNotFoundException; 进口java.io.FileOutputStream; 进口java.io.FileReader; 进口java.io.FileWriter; 进口java.io.IOException; 进口java.io.InputStream; 进口java.io.OutputStream; 进口java.io.Reader; 进口java.io.Writer; 公开课TestDemo { 私有静态int; 私有静态文件文件; 公共静态void main (String [] args) { fun2 (); } 公共静态孔隙有趣(){/* *初始化文件中的值为0 */尝试{ OutputStream=新FileOutputStream(文件); 字符串str=?0”; out.write (str.getBytes ()); out.close (); }捕捉(FileNotFoundException e) { e.printStackTrace (); }捕捉(IOException e) {//TODO自动生成的catch块 e.printStackTrace (); } } 公共静态孔隙fun2 () { 文件=新文件(“d:/测试/d.txt”); 如果(! file.exists ()) { 尝试{ file.createNewFile (); }捕捉(IOException e) {//TODO自动生成的catch块 e.printStackTrace (); } } 尝试{/* *读取文件中的内容*/如果(file.exists(),和file.length ()==0) { 有趣的(); } InputStream是=new FileInputStream(文件); 新字节字节b []=[(int) file.length ()); for (int i=0;我& lt;b.length;我+ +){//值字节在0 - 255范围之内是作为int来返回的 b[我]=(字节)is.read (); } 在=整数。方法(新的字符串(b)); + +; System.out.println(“读出来的" +);/* *再写入到文件中*/OutputStream=新FileOutputStream(文件); 字符串str=String.valueOf(的); byte[]字节=str.getBytes (); for (int i=0;我& lt;bytes.length;我+ +){ [我]out.write(字节);//一个字节一个字节的写入 } is.close (); out.close (); System.out.println(“写入的" +); }捕捉(FileNotFoundException e) { e.printStackTrace (); }捕捉(IOException e) { e.printStackTrace (); } } 公共静态孔隙fun3 () { 文件=新文件(“d:/测试/d.txt”); 如果(! file.exists ()) { 尝试{ file.createNewFile (); }捕捉(IOException e) {//TODO自动生成的catch块 e.printStackTrace (); } }/* *先读出来*/尝试{ 如果(file.exists(),和file.length ()==0) { 有趣的(); } 读者读者=new FileReader(文件); char [] c=new char [(int) file.length ()); int temp=0; int len=0; 在((temp=reader.read ()) !=1) { c (len)=(char)温度; len + +; } reader.close (); system . out。println(“初始值" +新字符串(c, 0, len)); 在=整数。方法(新字符串(c, 0, len)); + +; System.out.println(“下载一次:" +);/* *再写进去*/作家作家=new FileWriter(文件); writer.write (+ " "); writer.close (); System.out.println(“再写进去:" +); }捕捉(FileNotFoundException e) {//TODO自动生成的catch块 e.printStackTrace (); }捕捉(IOException e) {//TODO自动生成的catch块 e.printStackTrace (); } } 公共静态孔隙fun4 () { 读者读者; 作家的作家; 文件=新文件(“d:/测试/d.txt”); 如果(! file.exists ()) { 尝试{ file.createNewFile (); }捕捉(IOException e) {//TODO自动生成的catch块 e.printStackTrace (); } } 尝试{ 如果(file.exists(),和file.length ()==0) { 有趣的(); }/* *读出来*/读者=new FileReader(文件); BufferedReader br=new BufferedReader(读者); char [] c=new char [(int) file.length ()); int len=0; int temp=0; 在((temp=br.read ()) !=1){ c (len)=(char)温度; len + +; } 在=整数。方法(新字符串(c, 0, len)); + +; system . out。println(“读出来:" +);/* *写进去*/作家=new FileWriter(文件); BufferedWriter bw=new BufferedWriter(作家); bw.write (+ " "); System.out.println(“写进去:" +); br.close (); bw.close (); }捕捉(FileNotFoundException e) {//TODO自动生成的catch块 e.printStackTrace (); }捕捉(IOException e) {//TODO自动生成的catch块 e.printStackTrace (); } } }java使用文件流实现查看下载次数