我需要从位于每个jar根级别的属性文件“version.properties”中读取属性“product.build.number”。我天真的做法是:从jar中的属性文件读取特定属性
private static int getProductBuildNumber(File artefactFile) throws FileNotFoundException, IOException
{
try (ZipInputStream zip = new ZipInputStream(new FileInputStream(
artefactFile)))
{
Set<String> possClasses = new HashSet<>();
for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip
.getNextEntry())
{
if (!entry.isDirectory() && entry.getName().toLowerCase().equals(
"version.properties"))
{
List<String> lines = IOUtils.readLines(zip, (String) null);
for (String line : lines)
{
if (line.startsWith("product.build.number"))
{
String[] split = line.split("=");
if (split.length == 2)
{
return Integer.parseInt(split[1]);
}
}
}
}
}
}
throw new IOException("product.build.number not found.");
}
我想有更优雅和可靠的方法。有任何想法吗?
你的做法看起来是正确的,如果你是从罐子读取属性文件的类路径之外 – vsminkov