-3
import java.util.Properties;  
import java.io.*;  
public class ReadValues{  
   private static final String PROP_FILE="myConfig.properties";  
   public static void readPropertiesFile(){  
       try{  
         InputStream is = ReadValues.class.getResourceAsStream(PROP_FILE);  
         Properties prop = new Properties();  
             prop.load(is);  
         String directory = prop.getProperty("Directory");  
             String numberOfFiles = prop.getProperty("NumberOfFiles");  
         String  fileExtension = prop.getProperty("Extension");  
             is.close();  
      /* code to use values read from the file*/  
       }catch(Exception e){  
         System.out.println("Failed to read from " + PROP_FILE + " file.");  
       }  
   }  
  }  

Let me know how to retrieve the properties file from local drive in java.I do not want to hard code the drive name. for eg c:\app.properties i want this as output

peo
  • 11
  • 2
  • so.. you want to search all drives and folders for your properties file? – Matten Feb 02 '12 at 11:40
  • I believe OP wants to retrieve the drive letter from the current JVM Process' working directory. – Rekin Feb 02 '12 at 11:41
  • ya.....I want to search in all drive and load the properties file – peo Feb 02 '12 at 11:42
  • 1
    can't you decide the location of the properties file? how about put it in classpath? – Kent Feb 02 '12 at 11:43
  • no i do not want to mention the location.I want to have local drive and call it in java class? – peo Feb 02 '12 at 11:44
  • 4
    You should understand that "drive letter" is not an universal OS concept, and as such it has little meaning in a portable language as Java. You should use a some other criteria, like a relative path. – Viruzzo Feb 02 '12 at 11:45
  • can u please elaborate your comment... – peo Feb 02 '12 at 11:48
  • 1
    It's a bad idea to scan the entire computer for properties file. Do you know how long that will take? What if another program ***just happens*** to have a property file of the same name and you pick it up by mistake? You should either include the property file in your classpath, or ask the user to specify it via a command line property. – Perception Feb 02 '12 at 11:49
  • what is your requirement exactly? your java app will read a file, however for different env/user/.. the location of this file could be different. so you needs load the file dynamically? – Kent Feb 02 '12 at 11:51
  • if a properties file is in C:\\ drive how i can load the properties file without mentioning the driver name........... – peo Feb 02 '12 at 11:51
  • @Vidhya add another properties file (say p2) in your classpath. and write your C:\\.... path in p2. then in your java app read p2, get path. so, no C:\\ in your code. :D :D – Kent Feb 02 '12 at 11:54
  • My exact requirement is I want to load a property file which is present in local drive.I do not want to mention the driver name .How to do this? – peo Feb 02 '12 at 11:58
  • @kent where to add that another property file? – peo Feb 02 '12 at 12:01
  • @Kent in your classpath means you want me to add in src folder – peo Feb 02 '12 at 12:07

1 Answers1

1

well .. in a windows environment, you would first search for all your driver:

File[] roots = File.listRoots();
for(int i=0;i<roots.length;i++)
    System.out.println("Root["+i+"]:" + roots[i]);

After that, you will browse all locations and search for wanted files .. here's a starting point for you.

Java: Find .txt files in specified folder

Community
  • 1
  • 1
tartak
  • 485
  • 3
  • 17
  • My exact requirement is I want to load a property file which is present in local drive.I do not want to mention the driver name .How to do this? – peo Feb 02 '12 at 11:55
  • you are very ambiguous .. you want to search to find the files on local drive, you don't want to specify the drive.. I simply don't get what exactly do you want, sory. – tartak Feb 02 '12 at 12:59
  • _Why_ do you want to do this? What's so wrong with specifying the location of the file? What do you want to happen if there are several local drives? – DNA Feb 02 '12 at 13:31