import java.io.*;
import java.util.zip.*;
import java.nio.*;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
/**
* 压缩文件类
* @author winlin
*/
public class zipFile {
/**
* 普通写
*/
public void writeIt() throws IOException{
String dest="";
for(int i=0;i<5000;i++){
dest+="abcdefg";
}
//基础流
FileOutputStream fos=new FileOutputStream("d://test.java");
//滤流类
BufferedOutputStream bos=new BufferedOutputStream(fos);
//读写转换器
OutputStreamWriter osw=new OutputStreamWriter(bos);
//读写器
BufferedWriter bw=new BufferedWriter(osw);
//写
bw.write(dest);
bw.close();
}
/**
* 压缩文件
* @param path 文件路径
*/
public void zipIt(String path) throws IOException{
String dest="";
for(int i=0;i<5000;i++){
dest+="abcdefg";
}
//基础流
FileOutputStream fos=new FileOutputStream(path);
//派生流
GZIPOutputStream gzos=new GZIPOutputStream(fos);
//滤流类
BufferedOutputStream bos=new BufferedOutputStream(gzos);
//读写转换器
OutputStreamWriter osw=new OutputStreamWriter(bos);
//读写器
BufferedWriter bw=new BufferedWriter(osw);
//写
bw.write(dest);
}
/**
* 读取字符串内容
*/
public void writeString() throws IOException{
String dest="2515424/n/r1254";
//基础流
StringBufferInputStream sbis=new StringBufferInputStream(dest);
//滤流类
BufferedInputStream bos=new BufferedInputStream(sbis);
//读写转换器
InputStreamReader isr=new InputStreamReader(bos);
//读写器
BufferedReader br=new BufferedReader(isr);
System.out.println(br.readLine());
}
/**
* 屏幕输入
* @throws IOException
*/
public void readLine() throws IOException{
//基础流
//System.in;
//滤流类
BufferedInputStream bis=new BufferedInputStream(System.in);
//读写转换器
InputStreamReader isr=new InputStreamReader(bis);
//读写器
BufferedReader br=new BufferedReader(isr);
//读
String dest=br.readLine();
if(dest!=null)
System.out.println(dest);
}
/**
* 新读写引擎
*/
public void newReader() throws IOException{
//缓冲器
ByteBuffer bb=ByteBuffer.allocate(1024);
//文件流
FileInputStream fis=new FileInputStream("d://test.java");
//通道
FileChannel fc=fis.getChannel();
//操作
while(fc.read(bb)!=-1){
//准备读取缓冲区
bb.flip();
//编码输出缓冲区内容
System.out.print(Charset.forName("gb2312").decode(bb).toString());
//清空缓冲区
bb.clear();
}
}
/**
* 新写对象
* @throws IOException
*/
public void newWriter() throws IOException{
//文件基础流
FileOutputStream fos=new FileOutputStream("d://test.txt");
//通道
FileChannel fc=fos.getChannel();
//操作
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String dest=br.readLine();
while(!dest.equals("exit")){
//写
fc.write(ByteBuffer.wrap(dest.getBytes("gb2312")));
//
dest=br.readLine();
}
fc.close();
}
/**
* 对象序列化流
* @throws Exception
*/
public void objectStream() throws Exception{
Persistance obj=new Persistance();
//基础流
FileOutputStream fos=new FileOutputStream("d://object.java");
//派生流
ObjectOutputStream oos=new ObjectOutputStream(fos);
//写
oos.writeObject(obj);
oos.close();
}
public void objectReader() throws Exception{
Persistance obj=null;
//基础流
FileInputStream fis = new FileInputStream("d://object.java");
//派生流
ObjectInputStream ois = new ObjectInputStream(fis);
//读
Object o=ois.readObject();
ois.close();
if(Persistance.class.isInstance(o))
{
obj=(Persistance)o;
System.out.println(obj.name);
}
}
}
//下面是类Persistance的序列化后的十六进制查看,和C++非常类似!
package cn.winlin.java.hospital;
import java.io.*;
/**
* 对象序列化
* @author winlin
*/
public class Persistance implements Serializable{
static final long serialVersionUID = -19851212L;
/**
* 名字
*/
public String name="WinLin";
}
Java中提供了读取压缩文件的包java.util.zip,可以读写zip文件和gzip文件。
下面是一段读取一个名为test.gz的gzip文件的测试程序:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;
public class Test{
public static void main(String [] args){
try {
BufferedReader gzipReader =
new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream("test.gz"))));
String line = gzipReader.readLine();
while(line != null){
System.out.println(line);
line= gzipReader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
001 package demo;
002
003 import java.io.FileInputStream;
004 import java.io.IOException;
005 import java.util.zip.ZipEntry;
006 import java.util.zip.ZipInputStream;
007
008 public class ViewTextFileInZipFile {
009
010 public static void main(String[] args) {
011 try {
012 // Open the ZIP file
013 String inFilename = args[0];
014 ZipInputStream in = new ZipInputStream(new FileInputStream(inFilename));
015
016 // Get the first entry
017 ZipEntry entry = in.getNextEntry();
018 while(entry != null) {
019 if (entry.getName().toLowerCase().equals(args[1])) {
020 break;
021 }
022 entry = in.getNextEntry();
023 }
024
025 StringBuffer sb = new StringBuffer();
026 byte[] buf = new byte[1024];
027 int len;
028 while ((len = in.read(buf)) >; 0) {
029 sb.append(new String(buf, 0, len));
030 }
031 System.out.println(sb.toString());
032
033 in.close();
034 } catch (IOException e) {
035 }
036 }
037 }