//一个jar包可以包含多个entry,这样就能实现下面功能 1.I/O 读文件流步骤 File file=new File(filePath); InputStreamReader read = new InputStreamReader(new FileInputStream(file)); BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = bufferedReader.readLine() package PlayJar; /** * 读取txt文件里的路径文件 * @author gjf */ import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class ReadFile{ public static List<String> readTxtFile(){ String filePath = "E:/Jar/prpall.txt"; List<String> list = new ArrayList<String>(); try { File file=new File(filePath); if(file.isFile() && file.exists()){ //判断文件是否存在 InputStreamReader read = new InputStreamReader(new FileInputStream(file)); BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while((lineTxt = bufferedReader.readLine()) != null){ list.add("E:/运维域/基线域04-B/prpall/modules/webapps/"+lineTxt); } System.out.println("list"+list); read.close(); }else{ System.out.println("找不到指定的文件"); } }catch(Exception e) { System.out.println("读取文件内容出错"); e.printStackTrace(); } return list; } } package PlayJar; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.jar.JarEntry; import java.util.jar.JarOutputStream; /** * 把单个txt里的路径文件打包 * @author pc */ public class PJar { public static void main(String[] args) throws IOException { FileInputStream fis = null; JarOutputStream jos = null; jos = new JarOutputStream(new FileOutputStream("E:/Jar/ppp.jar")); for (String s : ReadFile.readTxtFile()) { File file = new File(s);// 文件地址 fis = new FileInputStream(file); // 创建压缩文件中的条目,subString是为了截距掉前面的路径 JarEntry enttry = new JarEntry(file.getPath().substring(38)); System.out.println("创建压缩文件中的条目 :" + enttry); // 将创建好的条目加入到压缩文件中 jos.putNextEntry(enttry); // 写入当前条目所对应的具体内容 byte[] buff = new byte[1024]; int len = 0; while ((len = fis.read(buff)) != -1) { jos.write(buff, 0, len); } } fis.close(); jos.close(); } }
时间: 2024-11-05 19:39:28