🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 2-17.16、文件读写,实现一个计数器 ``` public int getNum(){ int i = -1; try{ String stri=""; BufferedReader in = new BufferedReader(new FileReader(f)); while((stri=in.readLine())!=null){ i = Integer.parseInt(stri.trim()); } in.close(); }catch(Exception e){ e.printStackTrace(); } return i; } public void setNum(){ int i = getNum(); i++; try{ PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter(f,false))); out.write(String.valueOf(i)); //可能是编码的原因,如果直接写入int的话,将出现java编码和windows编码的混乱,因此此处写入的是String out.close() ; }catch(Exception e){ e.printStackTrace(); } } ```