??? ??? /**
??? * 解压缩文件(压缩文件中只有一个文件的情况) ??? * */
??? public class ZipFileDemo2{
??? public static void main(String[] args) throws IOException{ ??? File file = new File(\ ??? File outFile = new File(\??? ZipFile zipFile = new ZipFile(file);
??? ZipEntry entry = zipFile.getEntry(\??? 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(); ??? } ??? }
【运行结果】: 解压缩之前:
这个压缩文件还是175字节 解压之后产生:
又回到了56字节,表示郁闷。
现在让我们来解压一个压缩文件中包含多个文件的情况吧 ZipInputStream类
当我们需要解压缩多个文件的时候,ZipEntry就无法使用了,如果想操作更加复杂的压缩文件,我们就必须使用ZipInputStream类
??? import java.io.File;
??? import java.io.FileInputStream; ??? import java.io.FileOutputStream; ??? import java.io.IOException; ??? import java.io.InputStream; ??? import java.io.OutputStream; ??? import java.util.zip.ZipEntry; ??? import java.util.zip.ZipFile;
??? import java.util.zip.ZipInputStream; ??? ??? /**
??? * 解压缩一个压缩文件中包含多个文件的情况 ??? * */
??? public class ZipFileDemo3{
??? public static void main(String[] args) throws IOException{ ??? File file = new File(\ ??? File outFile = null;
??? ZipFile zipFile = new ZipFile(file); ??? ZipInputStream FileInputStream(file));
??? ZipEntry entry = null; ??? InputStream input = null; ??? OutputStream output = null;
??? while((entry = zipInput.getNextEntry()) != null){
??? System.out.println(\解压缩\文件\ ??? outFile = new File(\??? 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();
zipInput
=
new
ZipInputStream(new
??? } ??? } ??? }
【运行结果】: 被解压的文件:
解压之后再D盘下会出现一个temp文件夹,里面内容:
PushBackInputStream回退流
??? import java.io.ByteArrayInputStream; ??? import java.io.IOException;
??? import java.io.PushbackInputStream; ??? ??? /**
??? * 回退流操作 ??? * */
??? public class PushBackInputStreamDemo{
??? public static void main(String[] args) throws IOException{ ??? String str = \ ??? 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); ??? } ??? } ??? } ??? }
【运行结果】: hello(回退,) rollenholt
??? /**
??? * 取得本地的默认编码 ??? * */
??? public class CharSetDemo{
??? public static void main(String[] args){ ??? System.out.println(\系
统
默
认
编
码
为
:
\
System.getProperty(\
??? } ??? }
【运行结果】: 系统默认编码为:GBK
乱码的产生:
??? import java.io.File;
??? import java.io.FileOutputStream; ??? import java.io.IOException; ??? import java.io.OutputStream; ??? ??? /**
??? * 乱码的产生 ??? * */
??? public class CharSetDemo2{
??? public static void main(String[] args) throws IOException{ ??? File file = new File(\??? OutputStream out = new FileOutputStream(file); ??? byte[] bytes = \你好\??? out.write(bytes);
+
??? out.close(); ??? } ??? }
【运行结果】: ??
一般情况下产生乱码,都是由于编码不一致的问题。
六
您所在的位置:开发 > Java > 热点推荐 > Java中的IO整理完整版(6)
Java中的IO整理完整版(6)
2011-09-19 16:17 Rollen Holt Rollen Holt 我要评论(9) 字号:T | T
本文章基本覆盖了java IO的全部内容,java新IO没有涉及,因为我想和这个分开,以突出那个的重要性,新IO哪一篇文章还没有开始写,估计很快就能和大家见面。照旧,文章依旧以例子为主,因为讲解内容的java书很多了,我觉的学以致用才是真。代码是写出来的,不是看出来的。
AD:
对象的序列化
对象序列化就是把一个对象变为二进制数据流的一种方法。
一个类要想被序列化,就行必须实现java.io.Serializable接口。虽然这个接口中没有任何方法,就如同之前的cloneable接口一样。实现了这个接口之后,就表示这个类具有被序列化的能力。
先让我们实现一个具有序列化能力的类吧:
? ?
import java.io.*; /**