Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

I agree 100% with @ingo-burk @ingo-burk's answer. Here are a few points on top of that.

You could load the paths from a .properties file quite easily, for example given a properties file on the classpath:

# config.properties
paths = C:/xampp/xampp-control.exe|C:/Program Files/xampp/xampp-control.exe|C:/Program Files (x86)/xampp/xampp-control.exe

and this Java code:

private static final String PROPERTIES_FILENAME = "player.properties";
private static final String PATHS_PROPERTY = "paths";
private static final String ITEM_SEPARATOR = "|";
private String[] getPaths(String filename, String propName) throws IOException {
 InputStream input = this.getClass().getClassLoader().getResourceAsStream(filename);
 if (input == null) {
 return new String[0];
 }
 Properties prop = new Properties();
 prop.load(input);
 input.close();
 return prop.getProperty(propName, "").split(ITEM_SEPARATOR);
}
private String[] getPaths(String filename) throws IOException {
 return getPaths(filename, PATHS_PROPERTY);
}
String[] getPaths() throws IOException {
 return getPaths(PROPERTIES_FILENAME, PATHS_PROPERTY);
}

Notice that I changed all the path separators from \\ to /. Java figures out the correct path separator in the operating system, and it's simpler to type this way.

Btw, if the executable is always called xampp-control.exe then perhaps you can put that name in its own property, and remove the duplication from paths.

I agree 100% with @ingo-burk's answer. Here are a few points on top of that.

You could load the paths from a .properties file quite easily, for example given a properties file on the classpath:

# config.properties
paths = C:/xampp/xampp-control.exe|C:/Program Files/xampp/xampp-control.exe|C:/Program Files (x86)/xampp/xampp-control.exe

and this Java code:

private static final String PROPERTIES_FILENAME = "player.properties";
private static final String PATHS_PROPERTY = "paths";
private static final String ITEM_SEPARATOR = "|";
private String[] getPaths(String filename, String propName) throws IOException {
 InputStream input = this.getClass().getClassLoader().getResourceAsStream(filename);
 if (input == null) {
 return new String[0];
 }
 Properties prop = new Properties();
 prop.load(input);
 input.close();
 return prop.getProperty(propName, "").split(ITEM_SEPARATOR);
}
private String[] getPaths(String filename) throws IOException {
 return getPaths(filename, PATHS_PROPERTY);
}
String[] getPaths() throws IOException {
 return getPaths(PROPERTIES_FILENAME, PATHS_PROPERTY);
}

Notice that I changed all the path separators from \\ to /. Java figures out the correct path separator in the operating system, and it's simpler to type this way.

Btw, if the executable is always called xampp-control.exe then perhaps you can put that name in its own property, and remove the duplication from paths.

I agree 100% with @ingo-burk's answer. Here are a few points on top of that.

You could load the paths from a .properties file quite easily, for example given a properties file on the classpath:

# config.properties
paths = C:/xampp/xampp-control.exe|C:/Program Files/xampp/xampp-control.exe|C:/Program Files (x86)/xampp/xampp-control.exe

and this Java code:

private static final String PROPERTIES_FILENAME = "player.properties";
private static final String PATHS_PROPERTY = "paths";
private static final String ITEM_SEPARATOR = "|";
private String[] getPaths(String filename, String propName) throws IOException {
 InputStream input = this.getClass().getClassLoader().getResourceAsStream(filename);
 if (input == null) {
 return new String[0];
 }
 Properties prop = new Properties();
 prop.load(input);
 input.close();
 return prop.getProperty(propName, "").split(ITEM_SEPARATOR);
}
private String[] getPaths(String filename) throws IOException {
 return getPaths(filename, PATHS_PROPERTY);
}
String[] getPaths() throws IOException {
 return getPaths(PROPERTIES_FILENAME, PATHS_PROPERTY);
}

Notice that I changed all the path separators from \\ to /. Java figures out the correct path separator in the operating system, and it's simpler to type this way.

Btw, if the executable is always called xampp-control.exe then perhaps you can put that name in its own property, and remove the duplication from paths.

Source Link
janos
  • 113k
  • 15
  • 154
  • 396

I agree 100% with @ingo-burk's answer. Here are a few points on top of that.

You could load the paths from a .properties file quite easily, for example given a properties file on the classpath:

# config.properties
paths = C:/xampp/xampp-control.exe|C:/Program Files/xampp/xampp-control.exe|C:/Program Files (x86)/xampp/xampp-control.exe

and this Java code:

private static final String PROPERTIES_FILENAME = "player.properties";
private static final String PATHS_PROPERTY = "paths";
private static final String ITEM_SEPARATOR = "|";
private String[] getPaths(String filename, String propName) throws IOException {
 InputStream input = this.getClass().getClassLoader().getResourceAsStream(filename);
 if (input == null) {
 return new String[0];
 }
 Properties prop = new Properties();
 prop.load(input);
 input.close();
 return prop.getProperty(propName, "").split(ITEM_SEPARATOR);
}
private String[] getPaths(String filename) throws IOException {
 return getPaths(filename, PATHS_PROPERTY);
}
String[] getPaths() throws IOException {
 return getPaths(PROPERTIES_FILENAME, PATHS_PROPERTY);
}

Notice that I changed all the path separators from \\ to /. Java figures out the correct path separator in the operating system, and it's simpler to type this way.

Btw, if the executable is always called xampp-control.exe then perhaps you can put that name in its own property, and remove the duplication from paths.

lang-java

AltStyle によって変換されたページ (->オリジナル) /