zoukankan      html  css  js  c++  java
  • Properties类与配置文件

    //加载文件
    public
    static void testLoadProperties() throws Exception { Properties properties = new Properties(); InputStream in = new FileInputStream("E:/itcast/config.properties"); properties.load(in); // 加载 in.close(); System.out.println(properties); }
    //写配置文件
    public static void testStoreProperties() throws Exception {
    
        // 准备配置信息
    
        Properties properties = new Properties();
    
        properties.setProperty("name", "李四");
    
        properties.setProperty("age", "20");
    
     
    
        // 准备
    
        OutputStream out = new FileOutputStream("d:/my.properties");
    
        String comments = "这是我的配置文件";
    
     
    
        // 写出去
    
        properties.store(out, comments);
    
        out.close();
    
    }
    
    
    
     
     
    static Properties properties ;
        
        static{
            try {
                properties = new Properties();
                Class clazz = dd.class; 
                InputStream inputStream = clazz.getResourceAsStream("/my.properties"); //  "/"������Classpath��·����           getResourceAsStream �÷�������ʹ�õ�·������ʹ�������ļ�·����
                properties.load(inputStream);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        public static void main(String[] args) {
            System.out.println("��ǰ·����"+ new File(".").getAbsolutePath() );
            System.out.println( properties.getProperty("userName")+":"+properties.getProperty("password"));
            
        }

    Properties类对应.properties文件。文件内容是键值对,键值对之间使用"="或空格隔开。开头是"#"的表示注释

        Properties类在加载.properties文件时使用的iso8859-1的编码。所以这个文件中的中文要特殊处理:如果这个配置文件中有中文就必须要进行转义,使用native2ascii.exe命令操作:

    native2ascii d:/my.properties d:/my2.properties

       

        使用Properties类中的load(InputStream) 方法可以加载配置文件,使用其中的store(OutputStream) 方法可以保存配置到指定文件。

        更多的信息可以看Properties类的API文档。

    /**
             * 1. getRealPath读取,返回资源文件的绝对路径
             */
            /*String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
            System.out.println(path);
            File file = new File(path);
            FileInputStream in = new FileInputStream(file);*/
            
            /**
             * 2. getResourceAsStream() 得到资源文件,返回的是输入流
             */
            InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
            
            
            Properties prop = new Properties();
            //读取资源文件
            prop.load(in);
            
            String user = prop.getProperty("user");
            String password = prop.getProperty("password");
            System.out.println("user="+user);
            System.out.println("password="+password);
            
        }
  • 相关阅读:
    抽奖代码
    org.hibernate.AssertionFailure: null id in com.you.model.User entry (don't flush the Session after a
    Cannot add or update a child row: a foreign key constraint fails
    SyntaxError:identifier starts immediately after numeric literal
    too much recursion
    微信处理红包
    minerd
    minerd
    kill常用
    阿里云centos 6安装Nginx+PHP+MySQL
  • 原文地址:https://www.cnblogs.com/bee-home/p/7207634.html
Copyright © 2011-2022 走看看