无法删除Java TM Platform SE binary打开问题解决(修改jar中的文件)

               

参考文章:

http://www.2cto.com/kf/201204/126278.html

感恩楼主的分享

 private  void write2JarFile() { 
   String configPath = "property.properties"; 
    System.out.println("窗口关闭");
    File original=new File("E://license.jar"); 
       String originalPath = original.getAbsolutePath(); 
       
       System.out.println("originalPath==="+originalPath);
       /** 创建一个临时文件来做暂存,待一切操作完毕之后会将该文件重命名为原文件的名称(原文件会被删除掉)~ */ 
      String tempPath = originalPath.substring(0, originalPath.length()-4) + "_temp.jar"; 


       System.out.println(tempPath); 
        
       JarFile originalJar = null; 
       try { 
  //         originalJar = new JarFile(originalPath); 
        originalJar = new JarFile(originalPath); 
       } catch (IOException e1) { 
           e1.printStackTrace(); 
       } 
       List<JarEntry> lists = new LinkedList<JarEntry>(); 
       for(Enumeration<JarEntry> entrys = originalJar.entries(); entrys.hasMoreElements();) { 
           JarEntry jarEntry = entrys.nextElement(); 
//            System.out.println(jarEntry.getName());  
           lists.add(jarEntry); 
       } 
 
       // 定义一个 jaroutputstream 流  
       File handled = new File(tempPath); 
       JarOutputStream jos = null; 
       try { 
           FileOutputStream fos = new FileOutputStream(handled); 
           jos = new JarOutputStream(fos); 
            
           /**
            * 将源文件中的内容复制过来~
            * 可以利用循环将一个文件夹中的文件都写入jar包中 其实很简单
            */ 
           for(JarEntry je : lists) { 
               // jar 中的每一个文件夹 每一个文件 都是一个 jarEntry  
               JarEntry newEntry = new JarEntry(je.getName()); 
                
//              newEntry.setComment(je.getComment());  
//              newEntry.setCompressedSize(je.getCompressedSize());  
//              newEntry.setCrc(je.getCrc());  
//              newEntry.setExtra(je.getExtra());  
//              newEntry.setMethod(je.getMethod());  
//              newEntry.setTime(je.getTime());  
//              System.out.println(je.getAttributes());  
               /** 这句代码有问题,会导致将jar包重命名为zip包之后无法解压缩~ */ 
//              newEntry.setSize(je.getSize());  
                
               // 表示将该entry写入jar文件中 也就是创建该文件夹和文件  
               jos.putNextEntry(newEntry); 
                
               /** 如果当前已经处理到属性文件了,那么将在 JTextArea 中编辑过的文本写入到该属性文件~ */ 
               if(je.getName().equals(configPath)) { 
               String content="sss";
               System.out.println("content=="+content);
                   jos.write(content.getBytes()); 
                   continue; 
               } 
                
               InputStream is = originalJar.getInputStream(je); 
               byte[] bytes = inputStream2byteArray(is); 
               is.close(); 
                
               // 然后就是往entry中的jj.txt文件中写入内容  
               jos.write(bytes); 
           } 
        // 最后不能忘记关闭流  
           jos.close(); 
           fos.close();
           originalJar.close();


           
       } catch (Exception e) { 
           e.printStackTrace(); 
       }finally{
           /** 删除原始文件,将新生成的文件重命名为原始文件的名称~ */


           original.delete();
           handled.renameTo(new File(originalPath)); 
       } 
   } 




注意:除了关闭   jos.close(); 
            fos.close();这两个操作文件的流关闭之外,还要把  originalJar.close(); 执行这个关闭代码,否则就会报

无法删除Java(TM) Platform SE binary打开。删除文件之前,对所以操作该文件的流多要关闭掉。

           

猜你喜欢

转载自blog.csdn.net/qq_44910516/article/details/89279318
tm
今日推荐