今天写ComFrame属性配置工具,参照网上的资料,实现了题目的功能。
支持原创啊。
这里的流程是这样的。层层剥皮。
//------------------
读取jar文件里面的文件
//----------------
import java.io.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class ReadFileImpl implements IReadFile{
public String readFile() {
String result = "";
try {
String currentJarPath = ReadFileImpl.class.getProtectionDomain().getCodeSource().getLocation().getFile();
JarFile currentJar = new JarFile("conf.jar");
JarEntry dbEntry = currentJar.getJarEntry("text.properties");
InputStream in = currentJar.getInputStream(dbEntry);
//FileInputStream fis = new FileInputStream(currentJarPath+"/text.properties");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String readLine = null;
while((readLine = br.readLine())!= null){
result = result + readLine;
}
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return result;
}
}
//----------------------
解压某个文件 把conf.jar从UWFE.ear里面解压出来
//---------------------
JarFile currentJar = new JarFile(path + "/UWFE.ear");
JarEntry dbEntry = currentJar.getJarEntry("conf.jar");
InputStream in = currentJar.getInputStream(dbEntry);
FileOutputStream fos = new FileOutputStream(path+"/conff.jar", true);
int i = 0;
while ((i = in.read()) != -1) {
fos.write(i);
}
fos.flush();
fos.close();
//------------------------
打包 把conf.jar打包到UWFE.ear里面。
//------------------------
private static boolean EAR(){
try {
File tempJar = new File(filepath+"/UWFE1.ear");
tempJar.createNewFile();
JarFile jar = new JarFile(filepath+"/UWFE.ear");
FileOutputStream f = new FileOutputStream(tempJar);
JarOutputStream newJar = new JarOutputStream( f);
byte buffer[] = new byte[100];
int bytesRead;
// Add back the original files
Enumeration entries = jar.entries();
while (entries.hasMoreElements()) {
// Prompt for copy
JarEntry entry = (JarEntry) entries.nextElement();
if(entry.toString().equals("conf.jar"))
continue;
InputStream is = jar.getInputStream(entry);
newJar.putNextEntry(entry);
while ((bytesRead = is.read(buffer)) != -1) {
newJar.write(buffer, 0, bytesRead);
}
newJar.flush();
is.close();
} // Add new file last
jar.close();
JarEntry entry = new JarEntry("conf.jar");
newJar.putNextEntry(entry);
FileInputStream fis = new FileInputStream(filepath+"/conf.jar");
while ((bytesRead = fis.read(buffer)) != -1) {
newJar.write(buffer, 0, bytesRead);
}
newJar.flush();
fis.close();
newJar.close();
f.close();
return true;
} catch (Exception ex){
ex.getMessage();
return false;
}
}
如果需要转载,请标明出处,尊重偶的原创版权啊。
http://blog.csdn.net/keyboardsun
作者keyboardsun