有些算法能想到或者也能看懂但是当自己去实现时去不能写出来了,求高手指点迷津。谢谢!

解决方案 »

  1.   

    public class readPro 
    {
        
        public static void main(String[] args) throws IOException 
        {
            Properties pro = new Properties();
            FileInputStream fis = new FileInputStream("pro.properties");
            pro.load(fis);
        }}
      

  2.   

    package com.forum.utils;import java.util.HashMap;
    import java.util.Map;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.NodeList;public final class BeanFactory {
        
        private static Map<String, Object> map = null;
        private static String beanFile = "beanfactory.xml";
        private static String beanFilePath = Thread.currentThread().getContextClassLoader().getResource(beanFile).getPath(); 
        
        static {
            load();
        }
        public static Object getBean(String beanName) {
            return map.get(beanName);
        }
        
        private static void load() {
            map = new HashMap<String, Object>();
            try{
                Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(beanFilePath);
                NodeList daos = doc.getElementsByTagName("beans");
                int length = daos.getLength();
                Element e = null;
                for(int i=0; i<length; i++) {
                    e = (Element) daos.item(i);
                    map.put(e.getAttribute("name"), Class.forName(e.getAttribute("class")).newInstance());
                }
                e = null;
                daos =null;
                doc = null;
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
      

  3.   

    package com.forum.utils;import java.util.HashMap;
    import java.util.Map;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.NodeList;public final class BeanFactory {
        
        private static Map<String, Object> map = null;
        private static String beanFile = "beanfactory.xml";
        private static String beanFilePath = Thread.currentThread().getContextClassLoader().getResource(beanFile).getPath(); 
        
        static {
            load();
        }
        public static Object getBean(String beanName) {
            return map.get(beanName);
        }
        
        private static void load() {
            map = new HashMap<String, Object>();
            try{
                Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(beanFilePath);
                NodeList daos = doc.getElementsByTagName("beans");
                int length = daos.getLength();
                Element e = null;
                for(int i=0; i<length; i++) {
                    e = (Element) daos.item(i);
                    map.put(e.getAttribute("name"), Class.forName(e.getAttribute("class")).newInstance());
                }
                e = null;
                daos =null;
                doc = null;
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }