您的位置:首页 > 编程语言 > Java开发

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();

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: