The Artima Developer Community
Sponsored Link

Java Answers Forum
how to use the '.ini' file in java program?

2 replies on 1 page. Most recent reply: Dec 4, 2002 5:44 PM by wdzwdz

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
cuiqh

Posts: 1
Nickname: cuiqh
Registered: Dec, 2002

how to use the '.ini' file in java program? Posted: Dec 3, 2002 10:51 PM
Reply to this message Reply
Advertisement
i am designing a communication program! there are many parameters which should be configged before starting the program ,that is using the 'config.ini' file to config some application parameters.And I heard of the java API --Properties may be deal with it ,but how to do it ? Please help me !!!


Antonas

Posts: 11
Nickname: tony
Registered: May, 2002

Re: how to use the '.ini' file in java program? Posted: Dec 4, 2002 5:09 AM
Reply to this message Reply
Properties properties = new Properties();
properties.load(new FileInputStream("your_file.txt"));

String param1 = properties.getProperty("Name_of_param1");

...

I your_file.txt should look like that

Name_of_param1 /usr/tmp

Hope it will help

wdzwdz

Posts: 3
Nickname: wdzwdz
Registered: Dec, 2002

Re: how to use the '.ini' file in java program? Posted: Dec 4, 2002 5:44 PM
Reply to this message Reply
package wdz200211151;

/**
* <p>Title: </p>
* <p>Description:Properties?????????????????????????????? </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company:netsky </p>
* <p>Date: 2002-11-15</p>
* @author wdz
* @version 1.0
*/
import java.util.*;
import java.io.*;
public class Untitled1 {

/** ????????????????
* Properties ??Hashtable????????????????????????????????????
* public class Properties extends Hashtable
*
* @param Properties ????????????????
* @param fileName ????????
* */
static public void saveProperties(Properties p,String fileName){
java.io.FileOutputStream out=null;
try {
out =new FileOutputStream(fileName) ;
}
catch(Exception e){
System.out.println("error create file error!"+e.getMessage());
}

try{
p.store(out,"saveproperty");
}
catch(Exception e){
System.out.println("error save file error!"+e.getMessage());
}
}



/** ????????????????
* Properties ??Hashtable????????????????????????????????????
* public class Properties extends Hashtable
*
* @param fileName ????????
* @return ??????????????????
* */
static public Properties loadProperties(String fileName){
Properties p=new Properties();
FileInputStream in=null ;
try{
in = new FileInputStream(fileName);}
catch(Exception e){
System.out.println("error create file error!"+e.getMessage());
}

try {
p.load(in);
}
catch(Exception e){
System.out.println("error load file error!"+e.getMessage());
}
return p;
}

/**????????????????????????????????????
* Properties ??Hashtable????????????????????????????????????
* public class Properties extends Hashtable
*
* @param Properties Properties????
* @sKey ????????
* @return ??????????????
* */
static public String getAPropertyValue(Properties p,String sKey){
String sValue="";

if (p.containsKey(sKey)){
sValue = p.getProperty(sKey);
}

return sValue;

}

/**????????????????????????????????
* Properties ??Hashtable????????????????????????????????????
* public class Properties extends Hashtable
*
* @param fileName ??????????????
* @param sKey ????????
* @return ??????????????
* */
static public String getAPropertyValue(String fileName,String sKey){
String value="";
Properties wdzp = loadProperties( fileName);
value = getAPropertyValue(wdzp,sKey);
return value;
}

public static void main(String[] args) {
Untitled1 untitled11 = new Untitled1();
String fileName="wdzwdz.txt";

Properties p = new Properties();

p.put("author","wdz");
p.put("date","2002-11-15");
p.put("corp","netsky");
p.put("description","Properties??????! ");


//test save Properties
saveProperties(p,fileName);



//test load Properties
Properties pp =loadProperties(fileName);
System.out.println(pp);

//test load Property from Properties list
String author = getAPropertyValue(pp,"author");
System.out.println("author="+author);

//test load Property from a file
String corp = getAPropertyValue(fileName,"corp");
System.out.println("corp="+corp);
}
}

Flat View: This topic has 2 replies on 1 page
Topic: Help Previous Topic   Next Topic Topic: help with a error

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use