lundi 27 août 2018

File Not Found Exception with Config File

I am creating web application which requires Jdbc connectivity. I want to give the db details from a config file at the present working directory. For that i created a ReadConfigFile class. When i am running this class individually it gives me the correct present working directory. however when i m running the web application from the server, it shows me an incorrect present working directory and throws FileNotFound Exception. This is my code:

private static final String pwd=System.getProperty("user.dir");
private static final File configFile=new File(pwd+File.separator+"configFile.txt");

private static BufferedReader br;

public static String getDBHost() {
    String dbHost=null;
    try {
        br=new BufferedReader(new FileReader(configFile));
        String st;
        while((st=br.readLine())!=null) {
            if(st.trim().startsWith("DB Host: ")) {
                dbHost=st.trim().substring(9, st.trim().length());
            }
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        System.out.println(e);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.println(e);
    } finally {
        try {
            if(br!=null) {
                br.close();
            }               
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println(e);
        }
    }
    return dbHost;
}

Can anyone tell me what to do?




Aucun commentaire:

Enregistrer un commentaire