# werte aus einer ini-datei herauslesen..



## larss (24. April 2005)

hallo,

ich möchte variablen-werte aus einer ini-datei herauslesen, welche folgende syntax hat:

datei.ini:

#kommentar
variable1=20
variable2=30
variable3=40

die werte sind immer ganzzahlen im bereich von 0-127, welche jeweils einem byte entsprechen. kann man auch 2 werte von einer variablen herauslesen?

z.b.

#kommentar
variable1=20,30
variable2=40,50

ein beispiel dazu wäre super.. vielen dank!

gruß
lars


----------



## Bernd1984 (25. April 2005)

Hallo,

 hier der Code, den du suchst:



```
/*
   * Copyright (c) 2002 Stefan Matthias Aust.  All Rights Reserved.
   *
   * You are granted the right to use this code in a) GPL based projects in 
   * which case this code shall be also protected by the GPL, or b) in other 
   * projects as long as you make all modifications or extensions to this 
   * code freely available, or c) make any other special agreement with the 
   * copyright holder.
   */
  import java.io.*;
  import java.util.*;
  
  /**
   * This class can read properties files in Microsoft .ini file style and provides
   * an interface to read string, integer and boolean values.  The .ini files has the
   * following structure:
   * <pre>; a comment
   * [section]
   * key=value</pre>
   * 
   * @author Stefan Matthias Aust (sma@3plus4.de)
   * @version 1
   */
  public class IniReader {
  	private Map sections = new HashMap();
  
  	public IniReader(String pathname) throws FileNotFoundException, IOException {
  		this(new FileReader(pathname));
  	}
  	
  	public IniReader(InputStream input) throws FileNotFoundException, IOException {
  		this(new InputStreamReader(input));
  	}
  
  	public IniReader(Reader input) throws FileNotFoundException, IOException {
  		initialize(new BufferedReader(input));
  	}
  
  	private void initialize(BufferedReader r) throws IOException {
  		String section = null, line;
  		while ((line = r.readLine()) != null) {
  			line = line.trim();
  			if (line.equals(&quot;&quot;) || line.startsWith(&quot;;&quot;)) {
  				continue;
  			}
  			if (line.startsWith(&quot;[&quot;)) {
  				if (!line.endsWith(&quot;]&quot;)) {
  				    throw new IOException(&quot;] expected in section header&quot;);
  				}
  			    section = line.substring(1, line.length() - 1).toLowerCase();
  			} else if (section == null) {
  			    throw new IOException(&quot;[section] header expected&quot;);
  			} else {
  				int index = line.indexOf('=');
  				if (index < 0) {
  				    throw new IOException(&quot;key/value pair without =&quot;);
  				}
  			    String key = line.substring(0, index).trim().toLowerCase();
  			    String value = line.substring(index + 1).trim();
  			    Map map = (Map) sections.get(section);
  				if (map == null) {
  				    sections.put(section, (map = new HashMap()));
  				}
  				map.put(key, value);
  			}
  		}
  	}
  
  	public String getPropertyString(String section, String key, String defaultValue) {
  		Map map = (Map) sections.get(section.toLowerCase());
  		if (map != null) {
  			String value = (String) map.get(key.toLowerCase());
  			if (value != null) {
  				return value;
  			}
  		}
  		return defaultValue;
  	}
  
  	public int getPropertyInt(String section, String key, int defaultValue) {
  		String s = getPropertyString(section, key, null);
  		if (s != null) {
  			return Integer.parseInt(s);
  		}
  		return defaultValue;
  	}
  
  	public boolean getPropertyBool(String section, String key, boolean defaultValue) {
  		String s = getPropertyString(section, key, null);
  		if (s != null) {
  			return s.equalsIgnoreCase(&quot;true&quot;);
  		}
  		return defaultValue;
  	}
  
  	public static void main(String[] args) 
  	{
  		try 
  		{ 
  			IniReader v = new IniReader(&quot;d:\\text.ini&quot;); 
  		    System.out.println(&quot;Land: &quot; + v.getPropertyString(&quot;section&quot;,&quot;Land&quot;,&quot;fehlt&quot;));
  		    System.out.println(&quot;Stadt: &quot; + v.getPropertyString(&quot;section&quot;,&quot;Stadt&quot;,&quot;fehlt&quot;));
  		    System.out.println(&quot;Fluss: &quot; + v.getPropertyString(&quot;section&quot;,&quot;Fluss&quot;,&quot;fehlt&quot;));
  		    System.out.println(&quot;Bla Bla: &quot; + v.getPropertyString(&quot;section&quot;,&quot;Bla Bla&quot;,&quot;fehlt&quot;));
  		}
  		catch (Exception e) {		    e.printStackTrace();		}
  	}
  
  
  }
```


----------

