Java--学习笔记

image-20230522103205117

单例模式

public class Singleton {
	private static Singleton obj = new Singleton(); //共享同一个对象
	private String content;
	
	private Singleton()  //确保只能在类内部调用构造函数
	{
		this.content = "abc";
	}
	
	public String getContent() 	{
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}	
	
	public static Singleton getInstance()	{
		//静态方法使用静态变量
		//另外可以使用方法内的临时变量,但是不能引用非静态的成员变量
		return obj;
	}
	
	
	public static void main(String[] args) {
		Singleton obj1 = Singleton.getInstance();
		System.out.println(obj1.getContent());  //abc
		
		Singleton obj2 = Singleton.getInstance();
		System.out.println(obj2.getContent());  //abc
		
		obj2.setContent("def");
		System.out.println(obj1.getContent());
		System.out.println(obj2.getContent());
		
		System.out.println(obj1 == obj2); //true, obj1和obj2指向同一个对象
	}

}

常量池

public class BoxClassTest {
	public static void main(String[] args)
	{
		int i1 = 10;						//基本类型
		Integer i2 = 10;                // 自动装箱(包装类型)
		System.out.println(i1 == i2);   //true
		// 自动拆箱  基本类型和包装类进行比较,包装类自动拆箱
		
		Integer i3 = new Integer(10);
		System.out.println(i1 == i3);  //true
		// 自动拆箱  基本类型和包装类进行比较,包装类自动拆箱
		
		System.out.println(i2 == i3); //false
		// 两个对象比较,比较其地址。 
		// i2是常量,放在栈内存常量池中,i3是new出对象,放在堆内存中
		
		Integer i4 = new Integer(5);
		Integer i5 = new Integer(5);
		System.out.println(i1 == (i4+i5));   //true
		System.out.println(i2 == (i4+i5));   //true
		System.out.println(i3 == (i4+i5));   //true
		// i4+i5 操作将会使得i4,i5自动拆箱为基本类型并运算得到10. 
		// 基础类型10和对象比较, 将会使对象自动拆箱,做基本类型比较
		
		Integer i6 = i4 + i5;  // +操作使得i4,i5自动拆箱,得到10,因此i6 == i2.
		System.out.println(i1 == i6);  //true
		System.out.println(i2 == i6);  //true
		System.out.println(i3 == i6);  //false
	}	
}

public class StringNewTest {
	public static void main(String[] args) {
		String s0 = "abcdef";
		String s1 = "abc";
		String s2 = "abc";
		String s3 = new String("abc");
		String s4 = new String("abc");
		System.out.println(s1 == s2); //true 常量池
		System.out.println(s1 == s3); //false 一个栈内存,一个堆内存
		System.out.println(s3 == s4); //false 两个都是堆内存
		System.out.println("=========================");
		
		String s5 = s1 + "def";    //涉及到变量,故编译器不优化
		String s6 = "abc" + "def"; //都是常量 编译器会自动优化成abcdef
		String s7 = "abc" + new String ("def");//涉及到new对象,编译器不优化
		System.out.println(s5 == s6); //false
		System.out.println(s5 == s7); //false
		System.out.println(s6 == s7); //false
		System.out.println(s0 == s6); //true 
		System.out.println("=========================");

		
		String s8 = s3 + "def";//涉及到new对象,编译器不优化
		String s9 = s4 + "def";//涉及到new对象,编译器不优化
		String s10 = s3 + new String("def");//涉及到new对象,编译器不优化
		System.out.println(s8 == s9); //false
		System.out.println(s8 == s10); //false
		System.out.println(s9 == s10); //false
	}
}

异常

异常处理

  • try ... catch(catch可以有多个下同)
  • try..catch..finally
  • try..finally
  • try必须有,catch与finally至少有一个
  1. catch可以有多个,并且按照从上往下的优先级依次递减,只会进入一个catch块(try中发生的异常与catch的形参一致时)且执行完catch之后不会回到try中,一般小异常写在上面,大异常(宽泛的)写在后面。

  2. try、catch、finally中均可以继续嵌套try..catch语句

  3. 一个方法被覆盖,覆盖它的方法必须抛出相同的异常或是异常的子类。

  4. 如果父类抛出多个异常,那么重写的子类方法必须抛出这些异常的子集,即不能抛出新的异常。

自定义异常

自定义异常需要继承Exception类或其子类,

  • 继承自Exception,就变成Checked Exception
  • 继承自 RuntimeException,就变成Unchecked Exception

当某一函数的内部出现异常且catch中无法铺抓,则由谁调用该函数就由处理这个异常(即再由上一级处理这个异常)。

public class MyException extends Exception {

	private String returnCode ;  //异常对应的返回码
	private String returnMsg;  //异常对应的描述信息
	
	public MyException() {
		super();
	}

	public MyException(String returnMsg) {
		super(returnMsg);
		this.returnMsg = returnMsg;
	}

	public MyException(String returnCode, String returnMsg) {
		super();
		this.returnCode = returnCode;
		this.returnMsg = returnMsg;
	}

	public String getReturnCode() {
		return returnCode;
	}

	public String getreturnMsg() {
		return returnMsg;
	}
}

public class MyExceptionTest {
	public static void testException() throws MyException {
       throw new MyException("10001", "The reason of myException");
    }

	public static void main(String[] args) {
		//MyExceptionTest.testException();
		try {
			MyExceptionTest.testException();
		} catch (MyException e) {
			e.printStackTrace();
			System.out.println("returnCode:"+e.getReturnCode());
			System.out.println("returnMsg:"+e.getreturnMsg());
		}
	}
}

数据结构

List,列表

有序的Collection

允许重复元素

List 主要实现

ArrayList(非同步)

LinkdList(非同步)

Vector(同步)

image-20230526132242848 image-20230526132542136 image-20230526132605139

集合

image-20230526133031641 image-20230526133121476

HashSet

image-20230526133318245

遍历

Java Iterator(迭代器)不是一个集合,它是一种用于访问集合的方法,可用于迭代 ArrayListHashSet 等集合。

	public static void traverseByIterator(HashSet<Integer> hs)
	{
		long startTime = System.nanoTime();
		System.out.println("============迭代器遍历==============");
	    Iterator<Integer> iter1 = hs.iterator();
	    while(iter1.hasNext()){
	        iter1.next();
	    }
		long endTime = System.nanoTime();
	    long duration = endTime - startTime;
	    System.out.println(duration + "纳秒");
	}
	public static void traverseByFor(HashSet<Integer> hs)
	{
		long startTime = System.nanoTime();
		System.out.println("============for循环遍历==============");
	    for(Integer item : hs)
	    {
	    	;
	    }
		long endTime = System.nanoTime();
	    long duration = endTime - startTime;
	    System.out.println(duration + "纳秒");
	}

LinkHashSet

image-20230526133410489

遍历

	public static void traverseByIterator(LinkedHashSet<Integer> hs)
	{
		long startTime = System.nanoTime();
		System.out.println("============迭代器遍历==============");
	    Iterator<Integer> iter1 = hs.iterator();
	    while(iter1.hasNext()){
	        iter1.next();
	    }
		long endTime = System.nanoTime();
	    long duration = endTime - startTime;
	    System.out.println(duration + "纳秒");
	}
	public static void traverseByFor(LinkedHashSet<Integer> hs)
	{
		long startTime = System.nanoTime();
		System.out.println("============for循环遍历==============");
	    for(Integer item : hs)
	    {
	    	;
	    }
		long endTime = System.nanoTime();
	    long duration = endTime - startTime;
	    System.out.println(duration + "纳秒");
	}

TreeSet

image-20230526134442418

遍历

同LinkHashSet与LinkHashSet

集合判重复原则

image-20230526134542810

HashSet与LinkedHashSet的判重原则

class Dog {
    private int size;

    public Dog(int s) {
        size = s;
    }
    public int getSize() {
		return size;
	}

	public boolean equals(Object obj2) {
		System.out.println("Dog equals()~~~~~~~~~~~");
		if (0 == size - ((Dog) obj2).getSize()) {
			return true;
		} else {
			return false;
		}
	}

	//重写Object中的方法,为了是set能去重
	public int hashCode() {
		System.out.println("Dog hashCode()~~~~~~~~~~~");
		return size;
	}

	//重写Object中的方法,为了是set能去重
    public String toString() {
    	System.out.print("Dog toString()~~~~~~~~~~~");
        return size + "";
    }
}

		HashSet<Dog> hs = new HashSet<Dog>();  
		hs.add(new Dog(2));

TreeSet判重原则

public class Tiger implements Comparable{
	private int size;
	 
    public Tiger(int s) {
        size = s;    
    }    
    
    public int getSize() {
		return size;
	}
    
	public int compareTo(Object o) {
    	System.out.println("Tiger compareTo()~~~~~~~~~~~");
        return size - ((Tiger) o).getSize();
    }
}

		TreeSet<Tiger> ts = new TreeSet<Tiger>();  
		ts.add(new Tiger(2));

映射 Map

image-20230526140933392

Hashtable

image-20230526144753106
遍历
	public static void traverseByEntry(Hashtable<Integer,String> ht)
	{
		long startTime = System.nanoTime();
		System.out.println("============Entry迭代器遍历==============");
		Integer key;
		String value;
        //同时获得所有的键值对
		Iterator<Entry<Integer, String>> iter = ht.entrySet().iterator();
		while(iter.hasNext()) {
		    Map.Entry<Integer, String> entry = iter.next();
		    // 获取key
		    key = entry.getKey();
		    // 获取value
		    value = entry.getValue();
		    //System.out.println("Key:" + key + ", Value:" + value);
		}
		long endTime = System.nanoTime();
	    long duration = endTime - startTime;
	    System.out.println(duration + "纳秒");
	}

	public static void traverseByKeySet(Hashtable<Integer,String> ht)
	{
		long startTime = System.nanoTime();
		System.out.println("============KeySet迭代器遍历==============");
		Integer key;
		String value;
		Iterator<Integer> iter = ht.keySet().iterator();
		while(iter.hasNext()) {
		    key = iter.next();
		    // 获取value
		    value = ht.get(key);
		    //System.out.println("Key:" + key + ", Value:" + value);
		}
		long endTime = System.nanoTime();
	    long duration = endTime - startTime;
	    System.out.println(duration + "纳秒");
	}

	public static void traverseByKeyEnumeration(Hashtable<Integer,String> ht)
	{
		long startTime = System.nanoTime();
		System.out.println("============KeyEnumeration迭代器遍历==============");
		Integer key;
		String value;
		Enumeration<Integer> keys = ht.keys();
		while(keys.hasMoreElements()) {
		    key = keys.nextElement();
		    // 获取value
		    value = ht.get(key);
		    //System.out.println("Key:" + key + ", Value:" + value);
		}
		long endTime = System.nanoTime();
	    long duration = endTime - startTime;
	    System.out.println(duration + "纳秒");
	}

HashMap

image-20230526144917974
遍历

无序

方法同Hashtable

LinkHashMap

image-20230526145036182
遍历的顺序和插入的顺序一致

方法同Hashtable

TreeMap

image-20230526154652900

遍历

按照大小或是compareTo方法规定

	public static void traverseByEntry(TreeMap<Integer,String> ht)
	{
		long startTime = System.nanoTime();
		System.out.println("============Entry迭代器遍历==============");
		Integer key;
		String value;
		Iterator<Entry<Integer, String>> iter = ht.entrySet().iterator();
		while(iter.hasNext()) {
		    Map.Entry<Integer, String> entry = iter.next();
		    // 获取key
		    key = entry.getKey();
		    // 获取value
		    value = entry.getValue();
		    //System.out.println("Key:" + key + ", Value:" + value);
		}
		long endTime = System.nanoTime();
	    long duration = endTime - startTime;
	    System.out.println(duration + "纳秒");
	}

	public static void traverseByKeySet(TreeMap<Integer,String> ht)
	{
		long startTime = System.nanoTime();
		System.out.println("============KeySet迭代器遍历==============");
		Integer key;
		String value;
		Iterator<Integer> iter = ht.keySet().iterator();
		while(iter.hasNext()) {
		    key = iter.next();
		    // 获取value
		    value = ht.get(key);
		    //System.out.println("Key:" + key + ", Value:" + value);
		}
		long endTime = System.nanoTime();
	    long duration = endTime - startTime;
	    System.out.println(duration + "纳秒");
	}

Properties

image-20230526145125248

//关于Properties类常用的操作
public class PropertiesTest {
    //根据Key读取Value
    public static String GetValueByKey(String filePath, String key) {
        Properties pps = new Properties();
        try {
            InputStream in = new BufferedInputStream (new FileInputStream(filePath));  
            pps.load(in); //所有的K-V对都加载了
            String value = pps.getProperty(key);
            //System.out.println(key + " = " + value);
            return value;
            
        }catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    //读取Properties的全部信息
    public static void GetAllProperties(String filePath) throws IOException {
        Properties pps = new Properties();
        InputStream in = new BufferedInputStream(new FileInputStream(filePath));
        pps.load(in); //所有的K-V对都加载了
        Enumeration en = pps.propertyNames(); //得到配置文件的名字
        
        while(en.hasMoreElements()) {
            String strKey = (String) en.nextElement();
            String strValue = pps.getProperty(strKey);
            //System.out.println(strKey + "=" + strValue);
        }
        
    }
    
    //写入Properties信息
    public static void WriteProperties (String filePath, String pKey, String pValue) throws IOException {
        File file = new File(filePath);
    	if(!file.exists())
    	{
    		file.createNewFile();
    	}
    	Properties pps = new Properties();
        
        InputStream in = new FileInputStream(filePath);
        //从输入流中读取属性列表(键和元素对) 
        pps.load(in);
        //调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。  
        //强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
        OutputStream out = new FileOutputStream(filePath);
        pps.setProperty(pKey, pValue);
        //以适合使用 load 方法加载到 Properties 表中的格式,  
        //将此 Properties 表中的属性列表(键和元素对)写入输出流  
        pps.store(out, "Update " + pKey + " name");
        out.close();
    }
    
    public static void main(String [] args) throws IOException{
    	System.out.println("写入Test.properties================");
        WriteProperties("Test.properties","name", "12345");
        
        System.out.println("加载Test.properties================");
        GetAllProperties("Test.properties");
        
        System.out.println("从Test.properties加载================");
        String value = GetValueByKey("Test.properties", "name");
        System.out.println("name is " + value);
    }
}

附:Map中的entrySet

Entry

由于Map中存放的元素均为键值对,故每一个键值对必然存在一个映射关系。
Map中采用Entry内部类来表示一个映射项,映射项包含Key和Value (我们总说键值对键值对, 每一个键值对也就是一个Entry)
Map.Entry里面包含getKey()和getValue()方法

entrySet

entrySet是 java中 键-值 对的集合,Set里面的类型是Map.Entry,一般可以通过map.entrySet()得到。

  • entrySet实现了Set接口,里面存放的是键值对。一个K对应一个V。

工具类

Arrays

image-20230526173427417

Collecions

处理对象为Collection及其子类

image-20230526193207459
直接比较

比较的对象本身必须含有compareTo方法,如下面的 Integer、String

        List<Integer> list = new ArrayList<Integer>();
        list.add(4);
        list.add(2);
        list.add(3);
        Collections.sort(list);
        for (int i : list) {
            System.out.println(i);
        }

        List<String> list2 = new ArrayList<String>();
        list2.add("a");
        list2.add("c");
        list2.add("b");
        Collections.sort(list2);
        for (String s : list2) {
            System.out.println(s);
        }
重载方法
    static public void main(String[] st) {
        Person p1 = new Person("ab", 2);
        p1.toString();
        Person p2 = new Person("ba", 1);
        p2.toString();
        
        List<Person> l3 = new ArrayList<Person>();
        l3.add(p1);
        l3.add(p2);
        
        //核心,实现了接口中的方法
        Collections.sort(l3,new Comparator<Person>() {
            @Override
            public int compare(Person p1, Person p2) {
                return p1.getAge() - p2.getAge();
            }
        });
        
        for (Person p : l3) {
            System.out.println(p.toString());
        }
    }

    static class Person {
        String name;
        int age;
        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
        public String getName() {
            return this.name;
        }
        public int getAge() {
            return this.age;
        }
        @Override
        public String toString() {
            return "Person [name=" + name + ", age=" + age + "]";
        }
    }

对象比较

image-20230526193241217
实现自定义类的比较

注意下面中的Arrays.sort

public class Person implements Comparable<Person> {				//可以直接修改的类(知道源码)
	String name;
	int age;

	public String getName() {
		return name;
	}

	public int getAge() {
		return age;
	}

	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}

	public int compareTo(Person another) {
		int i = 0;
		i = name.compareTo(another.name); // 使用字符串的比较
		if (i == 0) {
			// 如果名字一样,比较年龄, 返回比较年龄结果
			return age - another.age;
		} else {
			return i; // 名字不一样, 返回比较名字的结果.
		}
	}

	public static void main(String... a) {
		Person[] ps = new Person[3];
		ps[0] = new Person("Tom", 20);
		ps[1] = new Person("Mike", 18);
		ps[2] = new Person("Mike", 20);

		Arrays.sort(ps);								//源码中实现了 compareTo 方法
		for (Person p : ps) {
			System.out.println(p.getName() + "," + p.getAge());
		}
	}
}
public class Person2Comparator  implements Comparator<Person2> {
	public int compare(Person2 one, Person2 another) {
		int i = 0;
		i = one.getName().compareTo(another.getName());
		if (i == 0) {
			// 如果名字一样,比较年龄,返回比较年龄结果
			return one.getAge() - another.getAge();
		} else {
			return i; // 名字不一样, 返回比较名字的结果.
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Person2[] ps = new Person2[3];								//这里 Person2 为不知道源码的类
		ps[0] = new Person2("Tom", 20);
		ps[1] = new Person2("Mike", 18);
		ps[2] = new Person2("Mike", 20);

		Arrays.sort(ps, new Person2Comparator());			//sort 方法使用了自己创建的实现了 compare 方法的类
		for (Person2 p : ps) {
			System.out.println(p.getName() + "," + p.getAge());
		}
	}
}

文件读写

文件读

image-20230527223250127

文件结构BufferedReader(InputStreamReader(FileInputStream()))

public class TxtFileRead {
	public static void main(String[] args) {
		readFile1();
		System.out.println("===================");
		//readFile2(); //JDK 7及以上才可以使用
	}

	public static void readFile1() {
		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		try {
			fis = new FileInputStream("c:/temp/abc.txt"); // 节点类
			isr = new InputStreamReader(fis, "UTF-8"); // 转化类
			//isr = new InputStreamReader(fis);
			br = new BufferedReader(isr); // 装饰类
			// br = new BufferedReader(new InputStreamReader(new
			// FileInputStream("c:/temp/abc.txt")))
			String line;
			while ((line = br.readLine()) != null) // 每次读取一行
			{
				System.out.println(line);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				br.close(); // 关闭最后一个类,会将所有的底层流都关闭
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}

	public static void readFile2() {
		String line;
		//try-resource 语句,自动关闭资源
		try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("c:/temp/abc.txt")))) {
			while ((line = in.readLine()) != null) {
				System.out.println(line);
			}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
}

文件写

image-20230527231357160

public class TxtFileWrite {
	public static void main(String[] args) {
		writeFile1();
		System.out.println("===================");
		//writeFile2(); // JDK 7及以上才可以使用
	}

	public static void writeFile1() {
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		BufferedWriter bw = null;
		try {
			fos = new FileOutputStream("c:/temp/abc.txt"); // 节点类
			osw = new OutputStreamWriter(fos, "UTF-8"); // 转化类
			//osw = new OutputStreamWriter(fos); // 转化类
			bw = new BufferedWriter(osw); // 装饰类
			// br = new BufferedWriter(new OutputStreamWriter(new
			// FileOutputStream("c:/temp/abc.txt")))
			bw.write("我们是");
			bw.newLine();
			bw.write("Ecnuers.^^");
			bw.newLine();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				bw.close(); // 关闭最后一个类,会将所有的底层流都关闭
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}

	public static void writeFile2() {
		//try-resource 语句,自动关闭资源
		try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("c:/temp/abc.txt")))) {
			bw.write("我们是");
			bw.newLine();
			bw.write("Ecnuers.^^");
			bw.newLine();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}

ZIP文件读写

压缩
image-20230529080349906

单文件压缩

public class SingleFileZip{
    public static void main(String args[]) throws Exception{
        File file = new File("c:/temp/abc.txt") ;  // 定义要压缩的文件
        File zipFile = new File("c:/temp/single2.zip") ;   // 定义压缩文件名称

        InputStream input = new FileInputStream(file) ; // 定义文件的输入流
        ZipOutputStream zipOut = null ; // 声明压缩流对象
        zipOut = new ZipOutputStream(new FileOutputStream(zipFile)) ;
        zipOut.putNextEntry(new ZipEntry(file.getName())) ; // 设置ZipEntry对象
        zipOut.setComment("single file zip") ;  // 设置注释

        //压缩过程
        int temp = 0 ;
        while((temp=input.read())!=-1){ // 读取内容
            zipOut.write(temp) ;    // 压缩输出
        }
        input.close() ; // 关闭输入流
        zipOut.close() ;    // 关闭输出流

        System.out.println("single file zip done.");
    }
}

多文件压缩

public class MultipleFileZip{
	public static void main(String args[]) throws Exception{	// 所有异常抛出
		File file = new File("c:/temp/multiple") ;	// 定义要压缩的文件夹
		File zipFile = new File("c:/temp/multiple2.zip") ;	// 定义压缩文件名称
		
		InputStream input = null ;	// 定义文件输入流
		ZipOutputStream zipOut = null ;	// 声明压缩流对象
		zipOut = new ZipOutputStream(new FileOutputStream(zipFile)) ;
		zipOut.setComment("multiple file zip") ;	// 设置注释
		
		//开始压缩
		int temp = 0 ;
		if(file.isDirectory()){	// 判断是否是文件夹
			File lists[] = file.listFiles() ;	// 列出全部子文件
			for(int i=0;i<lists.length;i++){
				input = new FileInputStream(lists[i]) ;	// 定义文件的输入流
				zipOut.putNextEntry(new ZipEntry(file.getName()
					+File.separator+lists[i].getName())) ;	// 设置ZipEntry对象
				System.out.println("正在压缩" + lists[i].getName());
				while((temp=input.read())!=-1){	// 读取内容
					zipOut.write(temp) ;	// 压缩输出
				}
				input.close() ;	// 关闭输入流
			}
		}
		zipOut.close() ;	// 关闭输出流
		System.out.println("multiple file zip done.");
	}
}
解压

image-20230529080509206

单文件解压

public class SingleFileUnzip{
    public static void main(String args[]) throws Exception{
        //待解压文件, 需要从zip文件打开输入流,读取数据到java中
    	File zipFile = new File("c:/temp/single.zip") ;   // 定义压缩文件名称
        ZipInputStream input = null ;   // 定义压缩输入流
        input = new ZipInputStream(new FileInputStream(zipFile)) ;  // 实例化ZIpInputStream
        ZipEntry entry = input.getNextEntry() ; // 得到一个压缩实体
        System.out.println("压缩实体名称:" + entry.getName()) ;  //获取压缩包中文件名字

        //新建目标文件,需要从目标文件打开输出流,数据从java流入
        File outFile = new File("c:/temp/" + entry.getName());
        OutputStream out = new FileOutputStream(outFile) ;   // 实例化文件输出流
        int temp = 0 ;
        while((temp=input.read())!=-1){
            out.write(temp) ;
        }
        input.close() ;     // 关闭输入流
        out.close() ;   // 关闭输出流
        System.out.println("unzip done.") ;
    }
}

多文件解压

public class MultipleFileUnzip{  
    public static void main(String args[]) throws Exception{    
    	//待解压的zip文件,需要在zip文件上构建输入流,读取数据到Java中
        File file = new File("c:/temp/multiple.zip") ;   // 定义压缩文件名称  
        File outFile = null ;   // 输出文件的时候要有文件夹的操作  
        ZipFile zipFile = new ZipFile(file) ;   // 实例化ZipFile对象  
        ZipInputStream zipInput = null ;    // 定义压缩输入流  
        
        //定义解压的文件名
        OutputStream out = null ;   // 定义输出流,用于输出每一个实体内容  
        InputStream input = null ;  // 定义输入流,读取每一个ZipEntry  
        ZipEntry entry = null ; // 每一个压缩实体  
        zipInput = new ZipInputStream(new FileInputStream(file)) ;  // 实例化ZIpInputStream 
        
        //遍历压缩包中的文件
        while((entry = zipInput.getNextEntry())!=null){ // 得到一个压缩实体  
        	System.out.println("解压缩" + entry.getName() + "文件") ;  
            outFile = new File("c:/temp/" + entry.getName()) ;   // 定义输出的文件路径  
            if(!outFile.getParentFile().exists()){  // 如果输出文件夹不存在 
                outFile.getParentFile().mkdirs() ;   
                // 创建文件夹 ,如果这里的有多级文件夹不存在,请使用mkdirs()
                // 如果只是单纯的一级文件夹,使用mkdir()就好了
            }  
            if(!outFile.exists()){  // 判断输出文件是否存在
            	if(entry.isDirectory())
            	{
            		outFile.mkdirs();
            		System.out.println("create directory...");
            	}
            	else
            	{
            		outFile.createNewFile() ;   // 创建文件
            		System.out.println("create file...");
            	}                  
            }  
            if(!entry.isDirectory())
            {
            	input = zipFile.getInputStream(entry) ; // 得到每一个实体的输入流  
                out = new FileOutputStream(outFile) ;   // 实例化文件输出流  
                int temp = 0 ;  
                while((temp=input.read())!=-1){  
                    out.write(temp) ;  
                }  
                input.close() ;     // 关闭输入流  
                out.close() ;   // 关闭输出流  
            }
            
        }  
        input.close() ;  
    }  
}
posted @   Mercurows  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
点击右上角即可分享
微信分享提示