Click to See Complete Forum and Search --> : Properties import


vagasv1
10-31-2007, 07:34 AM
As a java newbie. I have written a class that will look at .properties file on my server and return it in a java.util.properties.

are there any pointers, comment or improvements that you proffessional Java developers could suggest?


/*
* Created on 31-Oct-2007
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.dewhirst.crystalReportsUtilities.Utilities;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
* @author paeast
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class PropertiesImport {

private static final String PROPERTIESFILEFOLDERLiveServer = "C:\\IBM\\PE\\AppServer\\properties\\Dewhirst_Apps\\Crystal_Reports\\";
private static final String PROPERTIESFILEFOLDERTestServer = "D:\\IBM\\PE\\AppServer\\properties\\Dewhirst_Apps\\Crystal_Reports\\";
private static final String PROPERTIESFILEFOLDERPC = "C:\\Java\\wsad\\CrystalReports\\";
private static final String FILEDOESNOTEXIST = "The properties file does not exist (Please contact your local IT team).";


public Properties getProperties(String fileName) throws IOException{

BufferedInputStream inputStream = null;

String fileNameLocation = PROPERTIESFILEFOLDERLiveServer + fileName +".properties";
File file = new File(fileNameLocation);

if (!file.exists()){

fileNameLocation = PROPERTIESFILEFOLDERTestServer + fileName +".properties";
file = new File(fileNameLocation);
}

if (!file.exists()){

fileNameLocation = PROPERTIESFILEFOLDERPC + fileName +".properties";
file = new File(fileNameLocation);
}

if (!file.exists()){

throw new IOException(FILEDOESNOTEXIST);
}

try {
Properties properties = new Properties();
inputStream = new BufferedInputStream(new FileInputStream(file));

properties.load(inputStream);

return properties;

} catch (IOException e) {
throw e;

} finally {
if (inputStream!=null){
inputStream.close();
}

}


}


}

slaughters
10-31-2007, 04:33 PM
Don't repeat the !file.exists if statement 3 seperate times.

Just shove the unique part of the fileNameLocation (PROPERTIESFILEFOLDERLiveServer, etc..) in an array and loop through it while !file.exists or you hit the length of the array. Not only will you prevent duplication of code, but it will be easier if you ever need to add (or remove) an additional file location.