我在网上找的读取ini文件的例子,但是每次执行都是null,请问ini文件里的内容该怎么写呢import java.io.BufferedReader;       
import java.io.FileReader;       
import java.io.IOException;       
import java.util.HashMap;       
import java.util.Properties;       
    
public class IniReader
{       
protected HashMap sections = new HashMap();       
    private transient String currentSecion;       
    private transient Properties current;       
    
    public IniReader(String filename) throws IOException
    {       
     BufferedReader reader = new BufferedReader(new FileReader(filename));       
        read(reader);       
        reader.close();       
    }       
    
    protected void read(BufferedReader reader) throws IOException
    {       
     String line;
     while((line = reader.readLine()) != null)
     {       
     parseLine(line);       
        }       
    }      
    
    protected void parseLine(String line)
    {       
     line = line.trim();       
        if(line.matches("\\[.*\\]"))     
        {       
         if (current != null)
         {       
         sections.put(currentSecion,current);       
         }       
            currentSecion = line.replaceFirst("\\[(.*)\\]","$1");       
            current = new Properties();       
        }
        else if(line.matches(".*=.*"))
        {       
         int i = line.indexOf('=');       
            String name = line.substring(0,i);       
            String value = line.substring(i+1);       
            current.setProperty(name,value);       
        }       
    }       
    
    public String getValue(String section,String name)
    {       
     Properties p = (Properties) sections.get(section);       
        if (p == null)
        {       
         return     null;       
        }       
    
        String value = p.getProperty(name);       
        return value;       
    }
    
    public static void main(String[] args) throws IOException
    {       
     IniReader reader = new IniReader("c:\\File.ini");       
        System.out.println(reader.getValue("MCI Extensions.BAK","asf"));
    }
}

解决方案 »

  1.   

    [AppCtrl]
    QihooHint=1[REG_TCP]
    HOST=reg2t.sandai.net
    PORT=5200[REG_UDP]
    HOST=reg2u.sandai.net
    PORT=6200
    INTERVAL=300
    DISUDPNUM=5
    类似吧,哪行报的异常?
      

  2.   

     红色的做一下修改就OK了。
    protected void parseLine(String line) 
        {      
        line = line.trim();      
            if(line.matches("\\[.*\\]"))    
            {    
              System.out.println("1");
               currentSecion = line.replaceFirst("\\[(.*)\\]","$1");      
                  current = new Properties();  
            if (current != null) 
            {      
            sections.put(currentSecion,current);      
            }      
                  
            } 
            else if(line.matches(".*=.*")) 
            {   
               System.out.println("2");
            int i = line.indexOf('=');      
                String name = line.substring(0,i);      
                String value = line.substring(i+1);   
                System.out.println("name="+name+"value="+value);
                current.setProperty(name,value);      
            }      
        }      
        File.ini内容[MCI Extensions.BAK]
    asf=你要的值
    liblist=libwhite.dat,
    libversion=1.0.1.1114,
    libdate=2008-10-22
    libver2=1.0.1.2121
    libdate2=2008-10-22
      

  3.   

    java文件:import java.io.BufferedReader;      
    import java.io.FileReader;      
    import java.io.IOException;      
    import java.util.HashMap;      
    import java.util.Properties;      
        
    public class IniReader 
    {      
        protected HashMap sections = new HashMap();
        private transient String currentSecion;      
        private transient Properties current;      
        
        public IniReader(String filename) throws IOException 
        {      
            BufferedReader reader = new BufferedReader(new FileReader(filename));      
            read(reader);      
            reader.close();      
        }      
        
        protected void read(BufferedReader reader) throws IOException 
        {      
            String line; 
            while((line = reader.readLine()) != null) 
            {
                parseLine(line);      
            }
            sections.put(currentSecion, current);
        }      
        
        protected void parseLine(String line) 
        {      
            line = line.trim();      
            if(line.matches("\\[.*\\]"))    
            {
                if (current != null) 
                {
                    sections.put(currentSecion,current);
                } 
                currentSecion = line.replaceFirst("\\[(.*)\\]","$1");      
                current = new Properties();
            } 
            else if(line.matches(".*=.*")) 
            {
                int i = line.indexOf('=');      
                String name = line.substring(0,i);      
                String value = line.substring(i+1);      
                current.setProperty(name,value);      
            }      
        }      
        
        public String getValue(String section,String name) 
        {
            Properties p = (Properties) sections.get(section);      
            if (p == null) 
            {
                return null;      
            }      
        
            String value = p.getProperty(name);      
            return value;      
        } 
        
        public static void main(String[] args) throws IOException 
        {      
            IniReader reader = new IniReader("c:\\File.ini");      
            System.out.println(reader.getValue("REG_UDP","PORT")); 
        } 
    }
    INI文件:
    [AppCtrl] 
    QihooHint=1 [REG_TCP] 
    HOST=reg2t.sandai.net 
    PORT=5200 [REG_UDP] 
    HOST=reg2u.sandai.net 
    PORT=6200 
    INTERVAL=300 
    DISUDPNUM=5 是正常的啊,能取到值的啊。