黑马程序员技术交流社区
标题:
代码分析?
[打印本页]
作者:
騛鹏
时间:
2013-3-26 16:11
标题:
代码分析?
本帖最后由 騛鹏 于 2013-3-26 18:45 编辑
import java.util.*;
import java.io.*;
class PropertiesFile
{
public static void main(String[] args)
{
Properties settings = new Properties();
//settings.load(new FileInputStream("f:\\day\\day6\\count.txt"));
try
{
settings.load(new FileInputStream("f:\\day\\day6\\count.txt"));
}
catch (Exception e)
{
settings.setProperty("Count",new Integer(0).toString());
}
int c = Integer.parseInt(settings.getProperty("Count"))+1;
System.out.println("zhe shi di"+c+"ci shiyong");
settings.put("Count",new Integer(c).toString());
try
{
settings.store(new FileOutputStream("f:\\day\\day6\\count.txt"),"this program is used:");
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
复制代码
当不使用try{} catch{} (第一个)而在函数抛异常时,无法运行,就是不能自动创建count.txt 。 为何?
catch (Exception e)
{
settings.setProperty("Count",new Integer(0).toString());
} 创建的文件吗?
作者:
高顺德
时间:
2013-3-26 16:35
当不使用try的时候抛出异常 无法运行并且不能自动创建count.txt 因为Properties类的load方法只是提供加载输入流而不能自动创建文件 如果文件不存在就会出现异常 另外 记得关闭流
作者:
騛鹏
时间:
2013-3-26 18:24
天下 发表于 2013-3-26 16:35
当不使用try的时候抛出异常 无法运行并且不能自动创建count.txt 因为Properties类的load方法只是提供加载 ...
那么 文件 是如何创建的呢
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2