欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java使用文件流實(shí)現(xiàn)查看下載次數(shù)

 更新時(shí)間:2018年07月18日 11:15:45   作者:xusheng_Mr  
這篇文章主要為大家詳細(xì)介紹了java使用文件流實(shí)現(xiàn)查看下載次數(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java使用文件流實(shí)現(xiàn)查看下載次數(shù)的具體代碼,供大家參考,具體內(nèi)容如下

需求:點(diǎn)擊一個(gè)按鈕的次數(shù)或者是展示文件,游戲被下載的次數(shù)

實(shí)現(xiàn):開辟一個(gè)流文件,用來保存被下載的次數(shù),然后讀文件中value,點(diǎn)擊一次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 {
      /** 讀取文件中的內(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]);  // 一個(gè)字節(jié)一個(gè)字節(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);
    /** 再寫進(jìn)去*/
      Writer writer = new FileWriter(file);
      writer.write(in+"");
      writer.close();
      System.out.println("再寫進(jìn)去:"+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);
      /** 寫進(jìn)去*/
      writer =new FileWriter(file);
      BufferedWriter bw = new BufferedWriter(writer);
      bw.write(in+"");
      System.out.println("寫進(jìn)去:"+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();
    }

  }  

}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • spring boot實(shí)現(xiàn)profiles動(dòng)態(tài)切換的示例

    spring boot實(shí)現(xiàn)profiles動(dòng)態(tài)切換的示例

    Spring Boot支持在不同的環(huán)境下使用不同的配置文件,該技術(shù)非常有利于持續(xù)集成,在構(gòu)建項(xiàng)目的時(shí)候只需要使用不同的構(gòu)建命令就可以生成不同運(yùn)行環(huán)境下war包,而不需要手動(dòng)切換配置文件。
    2020-10-10
  • springboot實(shí)現(xiàn)maven多模塊和打包部署

    springboot實(shí)現(xiàn)maven多模塊和打包部署

    本文主要介紹了springboot實(shí)現(xiàn)maven多模塊和打包部署,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • SpringBoot整合分布式鎖redisson的示例代碼

    SpringBoot整合分布式鎖redisson的示例代碼

    這篇文章主要介紹了SpringBoot整合分布式鎖redisson,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-02-02
  • android中判斷服務(wù)或者進(jìn)程是否存在實(shí)例

    android中判斷服務(wù)或者進(jìn)程是否存在實(shí)例

    本篇文章主要介紹了android中判斷服務(wù)或者進(jìn)程是否存在實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • Spring遠(yuǎn)程加載配置的實(shí)現(xiàn)方法詳解

    Spring遠(yuǎn)程加載配置的實(shí)現(xiàn)方法詳解

    這篇文章主要介紹了Spring遠(yuǎn)程加載配置的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-03-03
  • 解決Java Redis刪除HashMap中的key踩到的坑

    解決Java Redis刪除HashMap中的key踩到的坑

    這篇文章主要介紹了解決Java Redis刪除HashMap中的key踩到的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • JAVA中StackOverflowError錯(cuò)誤的解決

    JAVA中StackOverflowError錯(cuò)誤的解決

    這篇文章主要介紹了JAVA中StackOverflowError錯(cuò)誤的解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • SpringBoot使用Mybatis-Generator配置過程詳解

    SpringBoot使用Mybatis-Generator配置過程詳解

    這篇文章主要介紹了SpringBoot使用Mybatis-Generator配置過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • 解決在IDEA中創(chuàng)建多級(jí)package的問題

    解決在IDEA中創(chuàng)建多級(jí)package的問題

    這篇文章主要介紹了解決在IDEA中創(chuàng)建多級(jí)package的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Java中的關(guān)鍵字_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Java中的關(guān)鍵字_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    關(guān)鍵字也稱為保留字,是指Java語言中規(guī)定了特定含義的標(biāo)示符。對(duì)于保留字,用戶只能按照系統(tǒng)規(guī)定的方式使用,不能自行定義
    2017-04-04

最新評(píng)論