ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 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(); } } ```