Property File auslesen in JTextFiled

  • Themenstarter Themenstarter dhachim
  • Beginndatum Beginndatum
Status
Nicht offen für weitere Antworten.
D

dhachim

Gast
Hallo ich mal wieder.

Ich möchte gerne ein Property File der Form
Code:
AdminHost=si40058	
ServerName=local	
Dimension=Space
ttnr_datei=index.htm

einlesen. Mir ist die PropertyKlasse in der java.util bekannt. Wenn ich bei dem TestField meine Methode getAdminHost() aufrufe bekomme ich als Ergebnis nur 'null':
Code:
jTextField1.setText(ConfigRead.getAdminHost());
Irgendwas habe ich vergessen.

Es Folgt meine ReadKlasse
Code:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class ConfigRead {

	public static void main(String[] args) {
		}
	
	public ConfigRead(String string){
		super();
		fileName = string;
		propertyRead();
		
	}
	protected String fileName = "server.property" ;
	protected static String AdminHost = null;
	protected static String ServerName = null;
	protected static String Dimension = null;
	protected static String ttnr_datei = null;
	private Properties MyP;
	
	void propertyRead() {
		File fi;
		FileInputStream fis = null;
		MyP = new Properties();
		fi = new File(fileName);
		
		try {
			fis = new FileInputStream(fi);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			MyP.load(fis);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		AdminHost= MyP.getProperty("AdminHost","");
		ServerName= MyP.getProperty("ServerName","");
		Dimension= MyP.getProperty("Dimension","");
		ttnr_datei= MyP.getProperty("ttnr_datei","");
		
	}

	public static String getAdminHost() {
		return AdminHost;
	}

	public static String getDimension() {
		return Dimension;
	}

	public static String getServerName() {
		return ServerName;
	}

	public static String getTtnr_datei() {
		return ttnr_datei;
	}
	/**
	 * @param string
	 */
	public void setAdminHost(String string)
	{
        MyP.setProperty("AdminHost", string);
	}

	/**
	 * @param string
	 */
	public void setServerName(String string)
	{
        MyP.setProperty("ServerName", string);
	}

	/**
	 * @param string
	 */
	public void setDimension(String string)
	{
        MyP.setProperty("Dimension", string);
	}

	/**
	 * @param string
	 */
	public void setttnr_datei(String string)
	{
        MyP.setProperty("ttnr_datei", string);
	}
	

}

Hoffe dass mir jemand Helfen kann... werd noch ganz wirr... seit 2 Tagen häng ich da dran fest.
 
Hi,

du müsstest vielleicht mal eine Instanz von ConfigRead erzeugen !? Und du solltest dann die Werte auch aus dieser Instanz holen. Der Misch-Masch aus static und non-static ist gefährlich. Mach dein ConfigRead zu einem Singleton.

Gruß
Mag1c
 
Deine Properties sind erst eingelesen, wenn Du ein new Config... aufgerufen hast, da deine Lese-Methode im Konstruktor steckt.

Alternative pack diese Dinge in einen static-Block
Code:
static {
  // ...
}

Oder mache in Singleton daraus.
 
:toll: :toll: :toll: :toll: :toll:
Ich danke dir... hatte schon fast eine Glatze vom Haareraufen. Es funktioniert.

Ich habe vergessen eine Instanz zu erzeugen. Ich habe noch nicht viel mit Java gemacht, so dass mir der Einstieg immer wieder schwierigkeiten bereitet. Aber dieses forum ist ja sehr lehrreich.

Danke nochmal.
 
Hier das ganze mal als Singleton; gibt bestimmt noch andere/bessere Methoden es umzusetzen, nur mal um es zu veranschaulichen.:
Code:
class ConfigRead {

  private static ConfigRead instance;

  // Gibt eine Referenz auf einen ConfigRead zurück
  public static synchronized ConfigRead getInstance() {
    if( instance == null ) {
      instance = new ConfigRead();
    }
    return instance;
  }

  // Konstruktor; ist private, damit die Klasse 'von aussen' nicht instanziierbar ist.
  private ConfigRead() {
    propertyRead();
  }

  protected String fileName = "c:/server.property";
  private Properties MyP;

  void propertyRead() {
    File fi;
    FileInputStream fis = null;
    MyP = new Properties();
    fi = new File( fileName );

    try {
      fis = new FileInputStream( fi );
    } catch( FileNotFoundException e ) {
      e.printStackTrace();
    }

    try {
      MyP.load( fis );
    } catch( IOException e ) {
      e.printStackTrace();
    }

  }

  // Rückgabe kann direkt über die Properties erfolgen
  public String getAdminHost() {
    return MyP.getProperty("AdminHost");
  }

  // ...
}

Der Aufruf würde dann so aussehen:
Code:
ConfigRead.getInstance().getAdminHost()
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben