java文件的打包和解包,java的自动打包和解包

map接口中的put(k,v)方法中的v参数要求的是对象的,其一般用法:

import java.util.*;

public class TestMap {

public static void main(String args[]) {

Map m1 = new HashMap();

m1.put("one",new Integer(1));//v参数是整形对象对象

m1.put("two",new Integer(2));

m1.put("three",new Integer(3));

System.out.println(m1);

if(m1.containsKey("two")) {

int i = ((Integer)m1.get("two")).intValue();//强制转换为整形对象在得出值

System.out.println(i);

}

}

}

利用自动打包和解包就很方便了:

import java.util.*;

public class TestMap {

public static void main(String args[]) {

Map m1 = new HashMap();

m1.put("one", 1);//自动打包,注意与上面代码的区别

m1.put("two", 2);

m1.put("three", 3);

System.out.println(m1)

if(m1.containsKey("two")) {

int i = (Integer)m1.get("two");//自动解包

System.out.println(i);

}

}

}

题外话:强制转换的存在,导致了泛型的出现。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java编程可以利用`java.util.jar`下的`JarFile`和`JarEntry`类来实现jar的解包打包。 1. 解包jar文件: ```java import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import java.util.jar.JarEntry; import java.util.jar.JarFile; public class JarUnpacker { public static void unpack(String jarPath, String destPath) throws Exception { JarFile jarFile = new JarFile(jarPath); Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry jarEntry = entries.nextElement(); String fileName = jarEntry.getName(); File destFile = new File(destPath + File.separator + fileName); if (jarEntry.isDirectory()) { destFile.mkdirs(); } else { InputStream in = jarFile.getInputStream(jarEntry); OutputStream out = new FileOutputStream(destFile); byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } in.close(); out.close(); } } jarFile.close(); } } ``` 以上代码会将`jarPath`指定的jar文件解压到`destPath`指定的目录中。 2. 打包jar文件: ```java import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.jar.JarEntry; import java.util.jar.JarOutputStream; public class JarPacker { public static void pack(String sourcePath, String jarPath) throws Exception { JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(jarPath)); File sourceFile = new File(sourcePath); if (sourceFile.isDirectory()) { packDir(sourceFile, "", jarOut); } else { packFile(sourceFile, "", jarOut); } jarOut.close(); } private static void packDir(File dir, String parentPath, JarOutputStream jarOut) throws IOException { String dirPath = parentPath + dir.getName() + "/"; JarEntry jarEntry = new JarEntry(dirPath); jarOut.putNextEntry(jarEntry); File[] files = dir.listFiles(); if (files != null) { for (File file : files) { if (file.isDirectory()) { packDir(file, dirPath, jarOut); } else { packFile(file, dirPath, jarOut); } } } } private static void packFile(File file, String parentPath, JarOutputStream jarOut) throws IOException { JarEntry jarEntry = new JarEntry(parentPath + file.getName()); jarOut.putNextEntry(jarEntry); byte[] buffer = new byte[1024]; int len; FileInputStream in = new FileInputStream(file); while ((len = in.read(buffer)) > 0) { jarOut.write(buffer, 0, len); } in.close(); } } ``` 以上代码会将`sourcePath`指定的文件或目录打包成`jarPath`指定的jar文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值