国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

java使用文件流實現查看下載次數

2024-07-14 08:41:39
字體:
來源:轉載
供稿:網友

本文實例為大家分享了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();    }  }  }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁化县| 岚皋县| 江华| 天柱县| 固原市| 德保县| 灵寿县| 山西省| 梅河口市| 横峰县| 正镶白旗| 东丽区| 台江县| 安徽省| 兴山县| 固镇县| 大方县| 黄石市| 江阴市| 玉屏| 西乡县| 颍上县| 襄樊市| 江油市| 大兴区| 东阳市| 息烽县| 囊谦县| 双鸭山市| 辛集市| 新源县| 崇礼县| 寻甸| 江西省| 东方市| 南昌县| 廊坊市| 启东市| 常熟市| 高青县| 嘉禾县|