Java IO实战操作(四)

/**
	 * 文件压缩 ZipOutputStream类
	 * @throws IOException
	 */
	public void ZipOutputStreamFile() throws IOException {
		File file = new File("d:" + File.separator + "hello.txt");
		File zipFile = new File("d:" + File.separator + "hello.zip");
		InputStream input = new FileInputStream(file);
		ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(
				zipFile));
		zipOut.putNextEntry(new ZipEntry(file.getName()));
		// 设置注释
		zipOut.setComment("hello");
		int temp = 0;
		while ((temp = input.read()) != -1) {
			zipOut.write(temp);
		}
		input.close();
		zipOut.close();
	}

	/**
	 * 多个文件的压缩
	 * @throws IOException 
	 */
	public void ZipOutputStreamFiles() throws IOException {
		// 要被压缩的文件夹
		File file = new File("d:" + File.separator + "temp"); //temp文件夹下面的所有文件
		File zipFile = new File("d:" + File.separator + "zipFile.zip");
		InputStream input = null;
		ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
		zipOut.setComment("hello");
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			for (int i = 0; i < files.length; ++i) {
				input = new FileInputStream(files[i]);
				zipOut.putNextEntry(new ZipEntry(file.getName()
						+ File.separator + files[i].getName()));
				int temp = 0;
				while ((temp = input.read()) != -1) {
					zipOut.write(temp);
				}
				input.close();
			}
		}
		zipOut.close();
	}
	/**
	 * ZipFile演示
	 * @throws IOException 
	 * @throws ZipException 
	 */
	public void ZipFileDemo() throws ZipException, IOException{
		File file = new File("d:" + File.separator + "hello.zip");
		ZipFile zipFile = new ZipFile(file);
		System.out.println("压缩文件的名称为:" + zipFile.getName());  
	}
	
	/**
	 * 解压缩文件(压缩文件中只有一个文件的情况)
	 * @throws IOException 
	 * @throws ZipException 
	 */
	public void ZipFileOutOne() throws ZipException, IOException{
		File file = new File("d:" + File.separator + "hello.zip");
		File outFile = new File("d:" + File.separator + "unZipFile.txt");
		ZipFile zipFile = new ZipFile(file);
		ZipEntry entry = zipFile.getEntry("hello.txt");
		InputStream input = zipFile.getInputStream(entry);
		OutputStream output = new FileOutputStream(outFile);
		int temp = 0;
		while((temp = input.read()) != -1){
			output.write(temp);
			}          
		input.close();
		output.close();  
	}
	


/**
	 * ZipInputStream解压多个文件 
	 * @throws IOException 
	 * @throws ZipException 
	 */
	public void ZipInputStreams() throws ZipException, IOException{
		File file = new File("d:" + File.separator + "zipFile.zip");
		File outFile = null;
		ZipFile zipFile = new ZipFile(file);
		ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));
		ZipEntry entry = null;
		InputStream input = null;
		OutputStream output = null;
		while((entry = zipInput.getNextEntry()) != null){
			System.out.println("解压缩" + entry.getName() + "文件");
			outFile = new File("d:" + File.separator + entry.getName());
			if(!outFile.getParentFile().exists()){
				outFile.getParentFile().mkdir();
				}              
			if(!outFile.exists()){
				outFile.createNewFile();
				}              
			input = zipFile.getInputStream(entry);
			output = new FileOutputStream(outFile);
			int temp = 0;
			while((temp = input.read()) != -1){
				output.write(temp);
				}              
			input.close();
			output.close();  
		}
	}
	/**
	 * PushBackInputStream退回流 
	 * @throws IOException 
	 */
	public void PushBackInputStreamFile() throws IOException{
		String str = "hello,rollenholt";
		PushbackInputStream push = null;
		ByteArrayInputStream bat = null;
		bat = new ByteArrayInputStream(str.getBytes());
		push = new PushbackInputStream(bat);
		int temp = 0;
		while((temp = push.read()) != -1){ 
			if(temp == ','){   
				push.unread(temp);
				temp = push.read();
				System.out.print("(回退" + (char) temp + ") ");
				}else{
					System.out.print((char) temp);
					}
			}
		/**   * 取得本地的默认编码   * */ 
		System.out.println("系统默认编码为:" + System.getProperty("file.encoding"));
	  }
}
/**
* 文件序列化 
	 */
public void SserializeFile() throws FileNotFoundException, IOException,
			ClassNotFoundException {
		// 打开是乱码必须使用ObjectInputStream查看
		File file = new File("d:" + File.separator + "hello.txt");
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
				file));
		oos.writeObject(new SerializableDemo("rollen", 20));
		oos.close();
		/** * ObjectInputStream示范 * */
		ObjectInputStream input = new ObjectInputStream(new FileInputStream(
				file));
		Object obj = input.readObject();
		input.close();
		System.out.println(obj);
	}


	/** *****序列化与反序列化*********** */
	// 序列化
	public void ser() throws Exception {
		File file = new File("d:" + File.separator + "hello.txt");
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
				file));
		out.writeObject(new Person("rollen", 20));
		out.close();
	}
	// 反序列化
	public void dser() throws Exception {
		File file = new File("d:" + File.separator + "hello.txt");
		ObjectInputStream input = new ObjectInputStream(new FileInputStream(file));
		Object obj = input.readObject();
		input.close();
		System.out.println(obj);
	}

/** * 实现具有序列化能力的类 * */
class SerializableDemo implements Serializable {
	public SerializableDemo() {
	}
	public SerializableDemo(String name, int age) {
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "姓名:" + name + "  年龄:" + age;
	}
	private String name;
	private int age;
}
class Person implements Externalizable {
	public Person() {
	}
	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "姓名:" + name + "  年龄:" + age;
	}
	// 复写这个方法,根据需要可以保存的属性或者具体内容,在序列化的时候使用
	public void writeExternal(ObjectOutput out) throws IOException {
		out.writeObject(this.name);
		out.writeInt(age);
	}
	// 复写这个方法,根据需要读取内容 反序列话的时候需要
	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
		this.name = (String) in.readObject();
		this.age = in.readInt();
	}
	private String name;
	// 加上transient关键字可以不序列化
	// private transient String name;
	private int age;
/**
	 * 序列化一组对象
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		Student[] stu = { new Student("hello", 20), new Student("world", 30),
				new Student("rollen", 40) };
		ser(stu);
		Object[] obj = dser();
		for (int i = 0; i < obj.length; ++i) {
			Student s = (Student) obj[i];
			System.out.println(s);
		}
	}
// 序列化
	public static void ser(Object[] obj) throws Exception {
		File file = new File("d:" + File.separator + "hello.txt");
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
				file));
		out.writeObject(obj);
		out.close();
	}
	// 反序列化
	public static Object[] dser() throws Exception {
		File file = new File("d:" + File.separator + "hello.txt");
		ObjectInputStream input = new ObjectInputStream(new FileInputStream(file));
		Object[] obj = (Object[]) input.readObject();
		input.close();
		return obj;
	}
}
class Student implements Serializable {
	public Student() {
	}
	public Student(String name, int age) {
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "姓名:  " + name + "  年龄:" + age;
	}
	private String name;
	private int age;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值