- package test31;
- import java.awt.*;
- import javax.swing.*;
- public class Gr3d4a extends Gr3d1m {
- /**
- *方法说明:主方法
- *输入参数:
- *返回类型:
- */
- public static void main(String[] args){
- Gr3d4a G3 = new Gr3d4a();
- }
- /**
- *方法说明:构造器
- *输入参数:
- *返回类型:
- */
- public Gr3d4a() {
- setTitle("3D cube box");
- setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- addMouseListener(this);
- addMouseMotionListener(this);
- setBackground(new Color(128,128,255));
- setSize(350,350);
- show();
- }
- /**
- *方法说明:绘制正方体盒子,过载Gr3d1m中的方法
- *输入参数:
- *返回类型:
- */
- public void drawPG(Graphics g,double []x,double []y,
- double []z,int xp,int yp,Color co) {
- double x1,y1,z0;
- int len=x.length;
- double [] xw=new double[len];
- double [] yw=new double[len];
- int [] xx=new int [len];
- int [] yy=new int [len];
- final double RAD=Math.PI/180.0;
- double a=angX*RAD;
- double b=angY*RAD;
- double sinA=Math.sin(a),sinB=Math.sin(b);
- double cosA=Math.cos(a),cosB=Math.cos(b);
- for (int i=0; i<len; i++) {
- x1= x[i]*cosB+z[i]*sinB;
- z0=-x[i]*sinB+z[i]*cosB;
- y1= y[i]*cosA- z0*sinA;
- xx[i]=xp+(int)Math.rint(x1);
- yy[i]=yp-(int)Math.rint(y1);
- xw[i]=x1; yw[i]=y1;
- }
- if (Hvec(xw,yw) > 0) {
- g.setColor(co);
- g.fillPolygon(xx,yy,len);//填充的多边形
- }
- }
- /**
- *方法说明:消影处理,如果平面被遮蔽将不被绘制
- *输入参数:
- *返回类型:
- */
- double Hvec(double []x,double []y) {
- return(x[0]*(y[1]-y[2])+x[1]*(y[2]-y[0])+x[2]*(y[0]-y[1]));
- }
- }
- package test32;
- import java.util.*;
- import java.io.*;
- /**
- * Title: 标注输入输出
- * Description: 接收标准的键盘输入,和输出到屏幕。
- * Filename: standerdIO.java
- */
- public class standerdIO{
- /**
- *方法说明:主方法
- *输入参数:
- *返回类型:
- */
- public static void main(String[] args){
- Vector<Object> vTemp = new Vector<Object>();
- boolean flag = true;
- while(flag){
- System.out.print("input>");
- String sTemp ="";
- //读取输入,System.in表示接收键盘输入流
- BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
- try{
- //读取一行输入
- sTemp = stdin.readLine();
- }catch(IOException ie){
- System.err.println("IO error!");
- }
- //解析输入命令
- String sCMD="";
- String sContext="";
- int point = sTemp.indexOf(":");
- if(point==-1){
- sCMD = sTemp.trim();
- }else{
- sCMD = sTemp.substring(0,point);
- sContext = sTemp.substring(point+1);
- }
- //添加数据
- if(sCMD.equalsIgnoreCase("in")){
- if(sContext.equals("")){
- System.err.println("this command format is errer!");
- }else{
- vTemp.addElement(sContext);
- }
- }//查看结果
- else if(sCMD.equalsIgnoreCase("out")){
- for(int i=0;i<vTemp.size();i++){
- System.out.println(i+":"+vTemp.elementAt(i));
- }
- }//结束
- else if(sCMD.equalsIgnoreCase("quit")){
- flag=false;
- }
- else{
- System.err.println("this command don't run!");
- System.out.print("use: in:command");
- System.out.print("use: out");
- }
- }
- }
- }
- package test33;
- import java.io.*;
- /**
- * Title: 读取和写入文件
- * Description: 使用字节流方式操作文件,读取和写入文件。
- * Filename: CopyBytes.java
- */
- public class BAKCopyBytes {
- /**
- *方法说明:主方法
- *输入参数:
- *返回类型:
- */
- public static void main(String[] args) throws IOException {
- String sFile;
- String oFile;
- if(args.length<2){
- System.out.println("USE:java CopyBytes source file | object file");
- return;
- }else{
- sFile = args[0];
- oFile = args[1];
- }
- try{
- File inputFile = new File(sFile);//定义读取源文件
- File outputFile = new File(oFile);//定义拷贝目标文件
- //定义输入文件流
- FileInputStream in = new FileInputStream(inputFile);
- //将文件输入流构造到缓存
- BufferedInputStream bin = new BufferedInputStream(in);
- //定义输出文件流
- FileOutputStream out = new FileOutputStream(outputFile);
- //将输出文件流构造到缓存
- BufferedOutputStream bout = new BufferedOutputStream(out);
- int c;
- //循环读取文件和写入文件
- while ((c = bin.read()) != -1)
- bout.write(c);
- //关闭输入输出流,释放资源
- bin.close();
- bout.close();
- }catch(IOException e){
- //文件操作,捕获IO异常。
- System.err.println(e);
- }
- }
- }
- package test33;
- import java.io.*;
- /**
- * Title: 读取和写入文件
- * Description: 使用字节流方式操作文件,读取和写入文件。
- * Filename: CopyBytes.java
- */
- public class CopyBytes {
- /**
- *方法说明:主方法
- *输入参数:
- *返回类型:
- */
- public static void main(String[] args) throws IOException {
- String sFile;
- String oFile;
- if(args.length<2){
- System.out.println("USE:java CopyBytes source file | object file");
- return;
- }else{
- sFile = args[0];
- oFile = args[1];
- }
- try{
- File inputFile = new File(sFile);//定义读取源文件
- File outputFile = new File(oFile);//定义拷贝目标文件
- //定义输入文件流
- FileInputStream in = new FileInputStream(inputFile);
- //将文件输入流构造到缓存
- BufferedInputStream bin = new BufferedInputStream(in);
- //定义输出文件流
- FileOutputStream out = new FileOutputStream(outputFile);
- //将输出文件流构造到缓存
- BufferedOutputStream bout = new BufferedOutputStream(out);
- int c;
- //循环读取文件和写入文件
- while ((c = bin.read()) != -1)
- bout.write(c);
- //关闭输入输出流,释放资源
- bin.close();
- bout.close();
- }catch(IOException e){
- //文件操作,捕获IO异常。
- System.err.println(e);
- }
- }
- }
- package test34;
- import java.io.*;
- /**
- * Title: 文件的读取和写入(字符)
- * Description: 使用FileReader和FileWriter类,采用字符文件访问方式操作文件。
- * Filename:
- */
- public class BAKCopyChar {
- /**
- *方法说明:主方法
- *输入参数:
- *返回类型:
- */
- public static void main(String[] args) throws IOException {
- String sFile;
- String oFile;
- if(args.length<2){
- System.out.println("USE:java CopyChar source file | object file");
- return;
- }else{
- sFile = args[0];
- oFile = args[1];
- }
- try{
- File inputFile = new File(sFile);//定义读取的文件源
- File outputFile = new File(oFile);//定义拷贝的目标文件
- //定义输入文件流
- FileReader in = new FileReader(inputFile);
- //将文件输入流构造到缓存
- BufferedReader bin = new BufferedReader(in);
- //定义输出文件流
- FileWriter out = new FileWriter(outputFile);
- //将输出文件流构造到缓存
- BufferedWriter bout = new BufferedWriter(out);
- int c;
- //循环读取和输入文件。
- while ((c = bin.read()) != -1)
- bout.write(c);
- bin.close();
- bout.close();
- }catch(IOException e){
- //文件操作,捕获IO异常。
- System.err.println(e);
- }
- }
- }
- package test34;
- import java.io.*;
- /**
- * Title: 文件的读取和写入(字符)
- * Description: 使用FileReader和FileWriter类,采用字符文件访问方式操作文件。
- * Filename:
- */
- public class CopyChar {
- /**
- *方法说明:主方法
- *输入参数:
- *返回类型:
- */
- public static void main(String[] args) throws IOException {
- String sFile;
- String oFile;
- if(args.length<2){
- System.out.println("USE:java CopyChar source file | object file");
- return;
- }else{
- sFile = args[0];
- oFile = args[1];
- }
- try{
- File inputFile = new File(sFile);//定义读取的文件源
- File outputFile = new File(oFile);//定义拷贝的目标文件
- //定义输入文件流
- FileReader in = new FileReader(inputFile);
- //将文件输入流构造到缓存
- BufferedReader bin = new BufferedReader(in);
- //定义输出文件流
- FileWriter out = new FileWriter(outputFile);
- //将输出文件流构造到缓存
- BufferedWriter bout = new BufferedWriter(out);
- int c;
- //循环读取和输入文件。
- while ((c = bin.read()) != -1)
- bout.write(c);
- bin.close();
- bout.close();
- }catch(IOException e){
- //文件操作,捕获IO异常。
- System.err.println(e);
- }
- }
- }
- package test35;
- import java.io.*;
- import java.util.*;
- /**
- * Title: 文件操作
- * Description: 演示文件的删除和获取文件的信息
- * Filename:
- */
- public class fileOperation{
- /**
- *方法说明:删除文件
- *输入参数:String fileName 要删除的文件名
- *返回类型:boolean 成功为true
- */
- public boolean delFile(String fileName){
- try{
- //删除文件
- boolean success = (new File(fileName)).delete();
- if (!success) {
- System.out.println("delete file error!");
- return false;
- }else{
- return true;
- }
- }catch(Exception e){
- System.out.println(e);
- return false;
- }
- }
- /**
- *方法说明:获取文件信息
- *输入参数:String Name 文件名
- *返回类型:String[] 文件信息数组
- */
- public String[] getFileInfo(String Name){
- try{
- File file = new File(Name);
- //获取文件修改日期(返回的是句)
- long modifiedTime = file.lastModified();
- //获取文件长度(单位:Bite)
- long filesize = file.length();
- //测试文件是否可读
- boolean cr = file.canRead();
- //测试文件是否可写
- boolean cw = file.canWrite();
- //测试文件是否隐藏
- boolean ih = file.isHidden();
- String[] sTemp = new String[6];
- sTemp[0] = String.valueOf(filesize);
- sTemp[1] = getDateString(modifiedTime);
- sTemp[2] = String.valueOf(cr);
- sTemp[3] = String.valueOf(cw);
- sTemp[4] = String.valueOf(ih);
- sTemp[5] = String.valueOf(file.getCanonicalPath());
- return sTemp;
- }catch(Exception e){
- System.out.println(e);
- return null;
- }
- }
- /**
- *方法说明:将毫秒数字转换为日期
- *输入参数:mill 毫秒数
- *返回类型:String 字符 格式为:yyyy-mm-dd hh:mm
- */
- public static String getDateString(long mill)
- {
- if(mill < 0) return "";
- Date date = new Date(mill);
- Calendar rightNow = Calendar.getInstance();
- rightNow.setTime(date);
- int year = rightNow.get(Calendar.YEAR);
- int month = rightNow.get(Calendar.MONTH);
- int day = rightNow.get(Calendar.DAY_OF_MONTH);
- int hour = rightNow.get(Calendar.HOUR_OF_DAY);
- int min = rightNow.get(Calendar.MINUTE);
- return year + "-" + (month <10 ? "0" + month : "" + month) + "-"
- + (day <10 ? "0" + day : "" + day)
- + (hour <10 ? "0" + hour : "" + hour)+":"
- + (min <10 ? "0" + min : "" + min);
- }
- /**
- *方法说明:主方法
- *输入参数:
- *返回类型:
- */
- public static void main(String[] args){
- try{
- fileOperation fo = new fileOperation();
- if(args.length==0){
- return;
- }else{
- String cmd = args[0];
- if(cmd.equals("del")){
- boolean bdel = fo.delFile(args[1]);
- System.out.println(bdel);
- }else if(cmd.equals("info")){
- String[] sTemp = fo.getFileInfo(args[1]);
- for(int i=0;i<sTemp.length;i++)
- System.out.println(sTemp[i]);
- }
- }
- }catch(Exception e){
- return;
- }
- }
- }
- package test36;
- /**
- * Title: 目录操作
- * Description: 演示列目录下的文件,和移动一个目录
- * Filename: Dir.java
- */
- import java.io.*;
- public class Dir{
- /**
- *方法说明:实现目录列表
- *输入参数:
- *返回类型:
- */
- public String[] DirList(String pathName){
- try{
- File path = null;
- String[] fileList;
- //如果没有指定目录,则列出当前目录。
- if(pathName.equals(""))
- path = new File(".");
- else
- path = new File(pathName);
- //获取目录文件列表
- if(path.isDirectory())
- fileList = path.list();
- else
- return null;
- return fileList;
- }catch(Exception e){
- System.err.println(e);
- return null;
- }
- }
- /**
- *方法说明:移动一个目录
- *输入参数:String sou 源目录, String obj 新目录
- *返回类型:
- */
- public boolean DirMove(String sou, String obj){
- try{
- //检查源文件是否存在
- boolean sexists = (new File(sou)).isDirectory();
- if(!sexists) return false;
- boolean oexists = (new File(obj)).isDirectory();
- //目标目录不存在,建立一个
- if(!oexists){
- (new File(obj)).mkdirs();
- }
- File file = new File(sou);
- File dir = new File(obj);
- //移动目录
- boolean success = file.renameTo(new File(dir, file.getName()));
- if (!success) {
- System.out.println("copy error!");
- return false;
- }
- else return true;
- }catch(Exception e){
- System.out.println(e);
- return false;
- }
- }
- /**
- *方法说明:主方法
- *输入参数:
- *返回类型:
- */
- public static void main(String[] args){
- Dir d = new Dir();
- if(args.length==0){
- return;
- }else{
- String cmd = args[0];
- if(cmd.equals("list")){
- if(args.length!=2) return;
- String[] sTemp = d.DirList(args[1]);
- for(int i=0;i<sTemp.length;i++)
- System.out.println(sTemp[i]);
- }else if(cmd.equals("move")){
- if(args.length!=3) return;
- d.DirMove(args[1],args[2]);
- }
- }
- }
- }
- package test37;
- import java.io.*;
- /**
- * Title: 读取随机文件
- * Description: 演示使用RandomAccessFile类读取文件。
- * Filename: RandFile.java
- */
- public class RandFile{
- /**
- *方法说明:主方法
- *输入参数:
- *返回类型:
- */
- public static void main(String[] args){
- String sFile;
- if(args.length<1){
- System.out.println("USE:java RandFile fileName");
- return;
- }else{
- sFile = args[0];
- }
- //接受IOException异常
- try{
- //构造随机访问文件,使用可读写方式。
- RandomAccessFile rf = new RandomAccessFile(sFile, "rw");
- for(int i = 0; i < 10; i++)
- rf.writeDouble(i*1.414);
- rf.close();
- //构造一个随机访问文件,使用只读方式
- rf = new RandomAccessFile(sFile, "rw");
- rf.seek(5*8);
- rf.writeDouble(47.0001);
- rf.close();
- //构造一个随机文件访问文件,使用只读方式。
- rf = new RandomAccessFile(sFile, "r");
- for(int i = 0; i < 10; i++)
- System.out.println("Value " + i + ": " + rf.readDouble());
- rf.close();
- }catch(IOException e){
- System.out.println(e);
- }
- }
- }
- package test38;
- import java.io.File;
- import jxl.*;
- import jxl.write.*;
- /**
- * Title: 操作EXCEL文件
- * Description: 本实例演示使用jxl包实现对excel文件的操作
- * Filename: myExcel.java
- */
- public class myExcel{
- Workbook workbook;
- Sheet sheet;
- /**
- *方法说明:写入文件操作
- *输入参数:
- *返回类型:
- */
- public void write(){
- try{
- //创建一个可写入的excel文件对象
- WritableWorkbook workbook = Workbook.createWorkbook(new File("myfile.xls"));
- //使用第一张工作表,将其命名为“午餐记录”
- WritableSheet sheet = workbook.createSheet("午餐记录", 0);
- //表头
- Label label0 = new Label(0, 0, "时间");
- sheet.addCell(label0);
- Label label1 = new Label(1, 0, "姓名");
- sheet.addCell(label1);
- Label label2 = new Label(2, 0, "午餐标准");
- sheet.addCell(label2);
- Label label3 = new Label(3, 0, "实际费用");
- sheet.addCell(label3);
- //格式化日期
- jxl.write.DateFormat df = new jxl.write.DateFormat("yyyy-dd-MM hh:mm:ss");
- jxl.write.WritableCellFormat wcfDF = new jxl.write.WritableCellFormat(df);
- jxl.write.DateTime labelDTF = new jxl.write.DateTime(0, 1, new java.util.Date(), wcfDF);
- sheet.addCell(labelDTF);
- //普通字符
- Label labelCFC = new Label(1, 1, "riverwind");
- sheet.addCell(labelCFC);
- //格式化数字
- jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");
- WritableCellFormat wcfN = new WritableCellFormat(nf);
- jxl.write.Number labelNF = new jxl.write.Number(2, 1, 13.1415926, wcfN);
- sheet.addCell(labelNF);
- jxl.write.Number labelNNF = new jxl.write.Number(3, 1, 10.50001, wcfN);
- sheet.addCell(labelNNF);
- //关闭对象,释放资源
- workbook.write();
- workbook.close();
- }catch(Exception e){
- System.out.println(e);
- }
- }
- /**
- *方法说明:读取excel文件一行数据
- *输入参数:int row指定的行数
- *返回类型:String〔〕结果数组
- */
- public String[] readLine(int row){
- try{
- //获取数据表列数
- int colnum = sheet.getColumns();
- String[] rest = new String[colnum];
- for(int i = 0; i < colnum; i++){
- String sTemp = read(i,row);
- if(sTemp!=null)
- rest[i] = sTemp;
- }
- return rest;
- }catch(Exception e){
- System.out.println("readLine err:"+e);
- workbook.close();
- return null;
- }
- }
- /**
- *方法说明:读取excel的指定单元数据
- *输入参数:
- *返回类型:
- */
- public String read(int col, int row){
- try{
- //获得单元数据
- Cell a2 = sheet.getCell(col,row);
- String rest = a2.getContents();
- return rest;
- }catch(Exception e){
- System.out.println("read err:"+e);
- workbook.close();
- return null;
- }
- }
- /**
- *方法说明:主方法,演示程序用
- *输入参数:
- *返回类型:
- */
- public static void main(String[] arges){
- try{
- myExcel me = new myExcel();
- //生成一个可读取的excel文件对象
- me.workbook = Workbook.getWorkbook(new File("myfile.xls"));
- //使用第一个工作表
- me.sheet = me.workbook.getSheet(0);
- //读一行记录,并显示出来
- String[] ssTemp = me.readLine(1);
- for(int i=0;i<ssTemp.length;i++)
- System.out.println(ssTemp[i]);
- //写入数据
- me.write();
- me.workbook.close();
- }catch(Exception e){
- System.out.println(e);
- }
- }
- }
- package test39;
- import com.lowagie.text.*;
- import com.lowagie.text.pdf.*;
- import java.io.*;
- import java.awt.Color;
- /**
- * Title: 生成PDF文件
- * Description: 本实例通过使用iText包生成一个表格的PDF文件
- * Filename: myPDF.java
- */
- public class myPDF{
- /**
- *方法说明:写PDF文件
- *输入参数:
- *返回类型:
- */
- public void write(){
- try{
- Document document=new Document(PageSize.A4, 50, 50, 100, 50);
- Rectangle pageRect=document.getPageSize();
- PdfWriter.getInstance(document, new FileOutputStream("tables.pdf"));
- //创建汉字字体
- BaseFont bfSong = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
- Font fontSong = new Font(bfSong, 10, Font.NORMAL);
- // 增加一个水印
- try {
- Watermark watermark = new Watermark(Image.getInstance("test.jpg"), pageRect.left()+50,pageRect.top()-85);
- watermark.scalePercent(50);
- document.add(watermark);
- }catch(Exception e) {
- System.err.println("请查看文件“test.jpg”是否在正确的位置?");
- }
- // 为页增加页头信息
- HeaderFooter header = new HeaderFooter(new Phrase("Java实例一百例",fontSong), false);
- header.setBorder(2);
- header.setAlignment(Element.ALIGN_RIGHT);
- document.setHeader(header);
- // 为页增加页脚信息
- HeaderFooter footer = new HeaderFooter(new Phrase("第 ",fontSong),new Phrase(" 页",fontSong));
- footer.setAlignment(Element.ALIGN_CENTER);
- footer.setBorder(1);
- document.setFooter(footer);
- // 打开文档
- document.open();
- //构造表格
- Table table = new Table(4);
- table.setDefaultVerticalAlignment(Element.ALIGN_MIDDLE);
- table.setBorder(Rectangle.NO_BORDER);
- int hws[] = {10, 20, 10, 20,};
- table.setWidths(hws);
- table.setWidth(100);
- //表头信息
- Cell cellmain = new Cell(new Phrase("用户信息",new Font(bfSong, 10, Font.BOLD,new Color(0,0,255))));
- cellmain.setHorizontalAlignment(Element.ALIGN_CENTER);
- cellmain.setColspan(4);
- cellmain.setBorder(Rectangle.NO_BORDER);
- cellmain.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
- table.addCell(cellmain);
- //分表头信息
- Cell cellleft= new Cell(new Phrase("收货人信息",new Font(bfSong, 10, Font.ITALIC,new Color(0,0,255))));
- cellleft.setColspan(2);
- cellleft.setHorizontalAlignment(Element.ALIGN_CENTER);
- table.addCell(cellleft);
- Cell cellright= new Cell(new Phrase("订货人信息",new Font(bfSong, 10, Font.ITALIC,new Color(0,0,255))));
- cellright.setColspan(2);
- cellright.setHorizontalAlignment(Element.ALIGN_CENTER);
- table.addCell(cellright);
- //收货和订货人信息,表体内容
- table.addCell(new Phrase("姓名",fontSong));
- table.addCell(new Phrase("张三",fontSong));
- table.addCell(new Phrase("姓名",fontSong));
- table.addCell(new Phrase("李四",fontSong));
- table.addCell(new Phrase("电话",fontSong));
- table.addCell(new Phrase("23456789",fontSong));
- table.addCell(new Phrase("电话",fontSong));
- table.addCell(new Phrase("9876543",fontSong));
- table.addCell(new Phrase("邮编",fontSong));
- table.addCell(new Phrase("100002",fontSong));
- table.addCell(new Phrase("邮编",fontSong));
- table.addCell(new Phrase("200001",fontSong));
- table.addCell(new Phrase("地址",fontSong));
- table.addCell(new Phrase("北京西城区XX路XX号",fontSong));
- table.addCell(new Phrase("地址",fontSong));
- table.addCell(new Phrase("上海陆家嘴区XX路XX号",fontSong));
- table.addCell(new Phrase("电子邮件",fontSong));
- table.addCell(new Phrase("zh_san@hotmail.com",fontSong));
- table.addCell(new Phrase("电子邮件",fontSong));
- table.addCell(new Phrase("li_si@hotmail.com",fontSong));
- //将表格添加到文本中
- document.add(table);
- //关闭文本,释放资源
- document.close();
- }catch(Exception e){
- System.out.println(e);
- }
- }
- /**
- *方法说明:主方法
- *输入参数:
- *返回类型:
- */
- public static void main(String[] arg){
- myPDF p = new myPDF();
- p.write();
- }
- }
- package test40;
- //文件名:myZip.java
- import java.io.*;
- import java.util.*;
- import java.util.zip.*;
- /**
- * Title: 文件压缩和解压
- * Description: 使用ZipInputStream和ZipOutputStream对文件
- * 和目录进行压缩和解压处理
- * Filename: myZip.java
- */
- public class myZip{
- /**
- *方法说明:实现文件的压缩处理
- *输入参数:String[] fs 压缩的文件数组
- *返回类型:
- */
- public void ZipFiles(String[] fs){
- try{
- String fileName = fs[0];
- FileOutputStream f =
- new FileOutputStream(fileName+".zip");
- //使用输出流检查
- CheckedOutputStream cs =
- new CheckedOutputStream(f,new Adler32());
- //声明输出zip流
- ZipOutputStream out =
- new ZipOutputStream(new BufferedOutputStream(cs));
- //写一个注释
- out.setComment("A test of Java Zipping");
- //对多文件进行压缩
- for(int i=1;i<fs.length;i++){
- System.out.println("Write file "+fs[i]);
- BufferedReader in =
- new BufferedReader(
- new FileReader(fs[i]));
- out.putNextEntry(new ZipEntry(fs[i]));
- int c;
- while((c=in.read())!=-1)
- out.write(c);
- in.close();
- }
- //关闭输出流
- out.close();
- System.out.println("Checksum::"+cs.getChecksum().getValue());
- }catch(Exception e){
- System.err.println(e);
- }
- }
- /**
- *方法说明:解压缩Zip文件
- *输入参数:String fileName 解压zip文件名
- *返回类型:
- */
- public void unZipFile(String fileName){
- try{
- System.out.println("读取ZIP文件........");
- //文件输入流
- FileInputStream fi =
- new FileInputStream(fileName+".zip");
- //输入流检查
- CheckedInputStream csi = new CheckedInputStream(fi,new Adler32());
- //输入流压缩
- ZipInputStream in2 =
- new ZipInputStream(
- new BufferedInputStream(csi));
- ZipEntry ze;
- System.out.println("Checksum::"+csi.getChecksum().getValue());
- //解压全部文件
- while((ze = in2.getNextEntry())!=null){
- System.out.println("Reading file "+ze);
- int x;
- while((x= in2.read())!=-1)
- //这里是写文件,write是以byte方式输出。
- System.out.write(x);
- }
- in2.close();
- }catch(Exception e){
- System.err.println(e);
- }
- }
- /**
- *方法说明:读取Zip文件列表
- *输入参数:String fileName zip文件名
- *返回类型:Vector 文件列表
- */
- @SuppressWarnings("unchecked")
- public Vector<Object> listFile(String fileName){
- try{
- @SuppressWarnings("unused")
- String[] aRst=null;
- Vector<Object> vTemp = new Vector<Object>();
- //zip文件对象
- ZipFile zf = new ZipFile(fileName+".zip");
- Enumeration e = zf.entries();
- while(e.hasMoreElements()){
- ZipEntry ze2 = (ZipEntry)e.nextElement();
- System.out.println("File: "+ze2);
- vTemp.addElement(ze2);
- }
- return vTemp;
- }catch(Exception e){
- System.err.println(e);
- return null;
- }
- }
- /**
- *方法说明:主方法
- *输入参数:
- *返回类型:
- */
- public static void main(String[] args){
- try{
- String fileName = args[0];
- myZip myZip = new myZip();
- myZip.ZipFiles(args);
- myZip.unZipFile(fileName);
- Vector<Object> dd = myZip.listFile(fileName);
- System.out.println("File List: "+dd);
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }