java实现:对jar包里面配置文件的修改

package com.test.tool;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;

/**
 * jarPath: jar包所在路径 
 * jarFilePath: jar中想要修改文件所在的路径
 * 
 */
public class JarTool {

	public JarTool change(String jarPath, String jarFilePath, String ip)
			throws IOException {

		if (jarPath != null && jarFilePath != null && ip != null) {
			File file = new File(jarPath);
			JarFile jarFile = new JarFile(file);// 通过jar包的路径 创建 jar包实例
			JarEntry entry = jarFile
					.getJarEntry("META-INF/config/adapterconfig.json");// 通过某个文件在jar包中的位置来获取这个文件


			InputStream input = jarFile.getInputStream(entry); // 创建该文件输入流
			
			List<JarEntry> lists = new LinkedList<JarEntry>();
			for (Enumeration<JarEntry> entrys = jarFile.entries(); entrys.hasMoreElements();) {
				JarEntry jarEntry = entrys.nextElement();
				lists.add(jarEntry);
			}

			process(lists, entry, jarPath, jarFilePath, input, ip); // 修改文件内容
			jarFile.close();
		}

		return null;
	}

	private static void process(List<JarEntry> lists, JarEntry entry,
			String jarPath, String jarFilePath, InputStream input, String ip)
			throws IOException {
		InputStreamReader isr = new InputStreamReader(input);
		BufferedReader br = new BufferedReader(isr);
		StringBuffer buf = new StringBuffer();
		String line = null;

		while ((line = br.readLine()) != null) {
			// 此处根据实际需要修改某些行的内容
			if (line.trim().startsWith(
					"host:\"http://127.0.0.1:8080/server.do\"")) {
				buf.append("host:\"" + ip + "/server.do\",");
			} else if (line.trim()
					.startsWith("host:\"http://127.0.0.1:8080/\"")) {
				buf.append("host:\"" + ip + "/\",");
			}
			// 如果不用修改, 则按原来的内容回写
			else {
				buf.append(line);
			}
			buf.append(System.getProperty("line.separator"));
		}

		write(lists, entry, jarPath, buf.toString());// 将修改后的内容写入jar包中的指定文件

		br.close();
	}

	public static void write(List<JarEntry> lists, JarEntry entry,
			String jarPath, String content) throws IOException {

		JarOutputStream jos = null;
		FileOutputStream fos = new FileOutputStream(jarPath);
		jos = new JarOutputStream(fos);
		

		try {

			for (JarEntry je : lists) {
				JarEntry newEntry = new JarEntry(je.getName());
				
				jos.putNextEntry(newEntry);

				if (je.getName().equals("META-INF/config/adapterconfig.json")) {
					jos.write(content.getBytes());
					continue;
				}

			}

			// 将内容写入文件中

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 关闭流
			if (jos != null) {
				try {
					jos.close();
				} catch (IOException e) {
					jos = null;
				}
			}
		}
	}
}

上述代码中,红色标记的“change”方法为外界提供调用,只要传入相应的参数,就可以实现功能,因为本人想要修改配置文件中的ip地址,所以传入了ip,各位码农可以随意添加自己想要的配置。


码字不易,欲求好评,欢迎各位的斧正。


                
  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值