本帖最后由 xing_6 于 2009-08-22 15:16:11 编辑

解决方案 »

  1.   

    不可行,jar运行时是不能修改的。只能在创建外部文件,没别的办法。
      

  2.   

    JarEntry entry = new JarEntry("your file name");
    JarOutputStream out = new JarOutputStream(new FileOutputStream("your jar file's path"));
    out.putNextEntry(entry);    
    out.write("the data you want to be write".getBytes());
    out.flush();
    out.close();
      

  3.   

    当然行,我之前也是把程序的配置内容保存到jar包中。
      

  4.   

    如果可行的话,只有一种可能性
    就是读出所有的文件以及把新文件一起生成一个新.jar,再删旧.jar
    晕,那不是相当与在C盘装系统?
      

  5.   

    如果你是在担心jar包中的其它数据是否丢失了,那你的担心是对的,我之前的做法是把jar中的所有内容先存到Hashtable中(但不包括我想替换的条目),然后再重新写的jar中,同时也把想保存的条目写入,这样可以解决了。
      

  6.   

    非常感谢 zlshcsdb 的回复,我用你提供的方法试验了一下,添加上心的文件后原来的文件都丢失。怎样临时保存原来jar包中的文件?怎样再重写回去?你能说的再具体一点吗,最好有实例代码,谢谢!!!
      

  7.   

    以下是我用过的代码:Hashtable<String,byte[]> table = new Hashtable<String,byte[]>();
    BufferedInputStream bIn = null;
    try{
        JarFile jf = new JarFile("jar path");
        Enumeration<JarEntry> entries = jf.entries();
        /* 第一步:将除了你想修改的其它的entry都保存到table中 */
        while(entries.hasMoreElements())
        {
            JarEntry entry = entries.nextElement();
            if(entry.getName().indexOf(".") > -1)
            {
                if(!entry.getName().equals("the entry you want to saved"))
                {
                    bIn = new BufferedInputStream(jf.getInputStream(entry));
                    int len = bIn.available();
                    byte[] bt = new byte[len];
                    bIn.read(bt);
                    bIn.close();
                    table.put(entry.getName(), bt);
                }
            }
        }
        jf.close();
    }catch(Exception e)
    {
    }
    try{
        JarEntry entry = new JarEntry("the entry you want to saved");
        JarOutputStream out = new JarOutputStream(new FileOutputStream("jar path"));
                   
        /* 第二步:将你想修改的entry先写到流中 */
        out.putNextEntry(entry);    
        out.write("your new entry".getBytes());
        out.flush();
        
        /* 第三步:将保存在table中的其它entry写到流中 */
        Enumeration<String> names = table.keys();
        while(names.hasMoreElements())
        {
            String entryName = names.nextElement();
            entry = new JarEntry(entryName); 
            out.putNextEntry(entry);                
            out.write(table.get(entryName));
            out.flush();
        }                      
        out.close();
    }catch(Exception e)
    {
    }
      

  8.   

    感谢zlshcsdb 的回复,结贴送分。
      

  9.   

    但是如果能直接操作就好了,因为担心jar文件太大。