黑马程序员技术交流社区
标题:
,这两个地方所new的FileOutputStream 对Properties有什么影响,?
[打印本页]
作者:
NO?
时间:
2014-4-2 00:54
标题:
,这两个地方所new的FileOutputStream 对Properties有什么影响,?
import java.util.*;
import java.io.*;
class RunCount
{
public static void main(String[] args)
{
Properties prop = new Properties();
File file = new File("count.ini");
if(!file.exists())
{
try
{
file.createNewFile();
System.out.println("haha");
}
catch (IOException e)
{
throw new RuntimeException(e.toString()+"yichang");
}
}
FileInputStream fis = null;
FileOutputStream fos = null;
try
{
fis = new FileInputStream(file);
fos = new FileOutputStream(file); //为什么我在这里new 一个FileOutput类. 就无法使文件持久化..
// fos.write("temp=0".getBytes());
//fos.newLine();
// fos.flush();
//System.out.println(fis.toString());
System.out.println(file.length());
prop.load(fis);
int count = 0;
System.out.println(prop);
String value = prop.getProperty("temp");
/*if(value == null)
{
System.out.println(value);
fos.write("temp=0".getBytes());
}
else*/
if(value != null)
count = Integer.valueOf(value);
count++;
// System.out.println(prop);
prop.setProperty("temp",count+"");
// fos = new FileOutputStream(file); // 但我如果在这里new的话.文件能一直保持...
prop.store(fos,"");
prop.list(System.out);
}
catch (IOException e)
{
throw new RuntimeException(e.toString()+"文件不存在");
}
finally
{
if(fis != null)
{
try
{
fis.close();
}
catch (IOException e)
{
throw new RuntimeException(e.toString()+"haha");
}
}
if(fos != null)
{
try
{
fos.close();
}
catch (IOException e)
{
throw new RuntimeException(e.toString()+"Hhasdas");
}
}
}
}
}
复制代码
想问的问题是,这两个地方所new的FileOutputStream 对Properties有什么影响,?
文字表达不好..麻烦各位有时间看看.
作者:
月影释
时间:
2014-4-2 01:11
个人理解 你在第一个位置加载file进入输出流的时候 ,文件的属性还没有改变 ,自然将文件存入内存 在下次打开的时候 由于属性还是与你上次一样,所以属性值是不变的。而在第二个位置,每次执行 属性都会先进行变化,所以可以存储进内存中。个人理解,没有尝试,如有错误,可以一起讨论
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2