我必须更改.jar文件中.config文件的内容,我已经从.jar文件夹中复制了配置文件的内容,并在.jar文件位置外创建了一个.config文件。现在我创建了一个方法正在使用我的.jar-file
,.config file name
和inputstream to this newly created .config file
。该方法创建一个临时的.jar文件,其中包含从原始jar复制的内容,最后将temp .jar文件重命名为原始文件并删除原始文件。它工作正常,但创建的.jar文件已损坏,无法打开。可能是什么问题,这是我的代码......
def createNewJar(jarToEdit:String, entryToEdit: String, isp:InputStream) = {
val tempJar = File.createTempFile(jarToEdit, null, new File("/home/administrator/Sources/Scala/rationaltab-launchpad"))
val jar = new JarFile(jarToEdit)
val entries = jar.entries()
val buffer = new Array[Byte](1024)
val newJar = new JarOutputStream(new FileOutputStream(tempJar))
var delFlag = false
var bytesRead = 0
while(entries.hasMoreElements) {
try {
val entry = entries.nextElement()
val is = jar.getInputStream(entry)
var newEntry = entry
if (newEntry.getName.equals(entryToEdit)) {
newEntry = new JarEntry(entryToEdit)
}
newJar.putNextEntry(newEntry)
if (!newEntry.getName.equals(entryToEdit)) {
while ( {bytesRead = is.read(buffer); bytesRead != -1 }) {
newJar.write(buffer, 0, bytesRead)
}
}
if (newEntry.getName.equals(entryToEdit)) {
while ( { bytesRead = isp.read(buffer); bytesRead != -1 }) {
newJar.write(buffer, 0, bytesRead)
}
}
newJar.closeEntry()
} catch { case e :Exception => e.printStackTrace() }
}
delFlag = true
if (delFlag) {
val origFile = new File(jarToEdit)
origFile.delete()
tempJar.renameTo(origFile)
}
}