欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2021-01-08 12:21:40|栏目:JAVA代码|点击:

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

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

实现:开辟一个流文件,用来保存被下载的次数,然后读文件中value,点击一次value加1,再将此value保存到流文件中。
三种方法:

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

  }  

}

上一篇:SpringBoot+Mybatis+Druid+PageHelper实现多数据源并分页方法

栏    目:JAVA代码

下一篇:Java语言资源国际化步骤解析

本文标题:java使用文件流实现查看下载次数

本文地址:http://www.codeinn.net/misctech/42379.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有