Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

search string in text file using JAVA

  Asked By: Francisca    Date: Mar 25    Category: Java    Views: 3498
  

I want to keep a text file for parameters for my java program. When I
access the java code, it will look for those parameters and store
them in variables.

For example:
PATH=c:\temp
CONNECT_STRING=xyz

Then these variables should be accessed at the begining of the java
program and stored in variables like vPath, vConnectStr etc.

Can anybody give me an idea? If there is a code available, it would
be very good to understand it.

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Eloise Lawrence     Answered On: Mar 25

Try using Properties.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File file  = null;

try {
// read the property text  file
file = new File("o:\\simulator\\myproperty.prop");
FileInputStream fis = new FileInputStream(file);

Properties prop = new Properties();
// feed the property with the file
prop.load(fis);

// Get the application to print out all the key and the value
// of your property file
Enumeration enum = prop.propertyNames();
while (enum.hasMoreElements()) {
String key = (String) enum.nextElement();
String value = (String) prop.get(key);
System.out.println(key + ":" + value);
}
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}

 
Answer #2    Answered By: Doyle Gonzalez     Answered On: Mar 25

Why don't you use a property file?
Look at the API docs, and search  for the PropertyResourceBundle class!

 
Answer #3    Answered By: Balbir Kaur     Answered On: Mar 25

FileInputStream in=new FileInputStream("<file name>");
Properties prop=new Properties();
prop.load(in);
in.close();
String path=prop.getProperty("PATH");
String connectt=prop.getProperty("CONNECT_STRING");

 
Didn't find what you were looking for? Find more on search string in text file using JAVA Or get search suggestion and latest updates.




Tagged: