黑马程序员技术交流社区
标题:
IO流中遇到了个异常疑惑的东西,求解决
[打印本页]
作者:
唐王潮
时间:
2014-10-8 12:31
标题:
IO流中遇到了个异常疑惑的东西,求解决
本帖最后由 唐王潮 于 2014-10-8 12:51 编辑
请先看完代码
package pro.properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
public class propertiesTest {
public static void main(String[] args) throws IOException {
/*
* 需求:定义一个功能,记录程序运行的次数。满足5此后,给出提示,试用次数已到,请注册!
*
*/
boolean b = checkCount();
if(b){
run();
}
}
private static boolean checkCount() throws IOException {
boolean isrun = true;
File configfile = new File("tempfile\\newinfo.propreties");
if(!configfile.exists()){
configfile.createNewFile();
}
int count = 0;
Properties prop = new Properties();
FileInputStream fis = new FileInputStream(configfile);
// FileOutputStream fos = new FileOutputStream(configfile);//为什么放到此处 写入配置文件中的count的值总是为1??????
prop.load(fis);
String value = prop.getProperty("count");
if(value!=null){
count = Integer.parseInt(value);
if(count>=5){
System.out.println("给钱");
return false;
}
}
count++;
prop.setProperty("count", Integer.toString(count));
FileOutputStream fos = new FileOutputStream(configfile);//放到此处没什么问题,正常
prop.store(fos,"app,time");
return isrun;
}
private static void run() {
System.out.println("程序运行");
}
}
复制代码
作者:
唐王潮
时间:
2014-10-8 19:41
zengming13 发表于 2014-10-8 15:15
因为你对一个文件同时打开输入流和输出流,导致后打开的流有效,先打开的输入流失效,导致配置文件不能正确 ...
非常感谢,根据你的回答,再加上自己的测试,确实如你所说,后打开的流会覆盖先打开的流。
作者:
琳以沫
时间:
2014-10-8 20:56
学习了。。。我也在学这块的知识了。。。
作者:
2406035442
时间:
2014-10-8 21:08
还没学。。。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2