黑马程序员技术交流社区

标题: Properties对象中,存盘时的小细节 [打印本页]

作者: ζ_____________    时间: 2014-9-28 23:48
标题: Properties对象中,存盘时的小细节
不多说,直接上代码.
class PropertiesDemo
{
        public static void main(String[] args)
        {
                loadDemo();
        }
        public static void loadDemo()
        {
                Properties prop = new Properties();
                FileInputStream fis = null;
                FileOutputStream fos = null;
                try
                {
                        fis = new FileInputStream("info.txt");
                       
                        prop.load(fis);                                                        //将info.txt的内容读取到内存中.

                        prop.setProperty("front","other");


       //所以不可以把这一行放在load方法之前,否则后果就是, 只有之后setProperty()里存放的键值了...
                        fos = new FileOutputStream("info.txt");        //创建(覆盖)一个新的info.txt(因为输出流就是这么霸道).


                        prop.store(fos,"biaozhu");

                }
                catch (IOException e)
                {
                        System.out.println(e.toString());
                }
                finally
                {
                        try
                        {
                                if (fis != null)
                                {
                                        fis.close();
                                }
                        }
                        catch (IOException e)
                        {
                                System.out.println(e.toString());
                        }
                        finally
                        {
                                try
                                {
                                        if (fos != null)
                                        {
                                                fos.close();
                                        }
                                }
                                catch (IOException e)
                                {
                                        System.out.println(e.toString());
                                }
                        }
                }
        }


作者: JerryJava    时间: 2014-9-29 06:45
细节决定一切, 多谢。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2