Java中用文本进行数据的读写
2016-03-12 17:55
423 查看
开始读取数据:
public static void start(){
Reader input = null;
BufferedReader br = null;
try {
input = new FileReader("record/MyRecord.txt");
br = new BufferedReader(input);
String str = br.readLine();
if(str == null){
str ="0";
}
score = Integer.parseInt(str);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
最后保存数据:
public static void end(){
Writer out = null;
BufferedWriter bw = null;
try {
out = new FileWriter("record/MyRecord.txt");
bw = new BufferedWriter(out);
bw.write(score+"\r\n");
} catch (IOException e) {
e.printStackTrace();
}finally{
if(bw!=null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void start(){
Reader input = null;
BufferedReader br = null;
try {
input = new FileReader("record/MyRecord.txt");
br = new BufferedReader(input);
String str = br.readLine();
if(str == null){
str ="0";
}
score = Integer.parseInt(str);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
最后保存数据:
public static void end(){
Writer out = null;
BufferedWriter bw = null;
try {
out = new FileWriter("record/MyRecord.txt");
bw = new BufferedWriter(out);
bw.write(score+"\r\n");
} catch (IOException e) {
e.printStackTrace();
}finally{
if(bw!=null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
相关文章推荐
- jdk1.8新特性详解
- Java设计模式透析之 —— 适配器(Adapter)
- spring4.2.5实现调度任务的几种方式
- 一个简单的Java IO流写的记事本
- Java 输入输出流巩固学习
- 十字链表(Java)
- Java IO(二)
- Java中 == 和equals的区别
- [Java语言] Spring+Mybatis+Velocity配置
- Java动态代理
- Java中的比较器
- [Java代码] 快速上手JFinal
- java 爬虫实现爬豆瓣电影数据并存入mysql数据库
- java实现随机洗牌算法
- java concurrent
- JAVA学习之多线程
- Leetcode:160. Intersection of Two Linked Lists(JAVA)
- 20145215《Java程序设计》第2周学习总结
- Spring 事务管理
- java SE多线程(关于runnable接口实现多线程)