100个Java经典例子(31-40)

[java]  view plain  copy
  1. package test31;  
  2.   
  3.  import java.awt.*;   
  4.  import javax.swing.*;  
  5.  public class Gr3d4a extends Gr3d1m {  
  6.    
  7. /** 
  8.  *方法说明:主方法 
  9.  *输入参数: 
  10.  *返回类型: 
  11.  */  
  12.   public static void main(String[] args){  
  13.      Gr3d4a G3 = new Gr3d4a();  
  14.   }  
  15. /** 
  16.  *方法说明:构造器 
  17.  *输入参数: 
  18.  *返回类型: 
  19.  */  
  20.   public  Gr3d4a() {  
  21.      setTitle("3D cube box");  
  22.      setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
  23.      addMouseListener(this);  
  24.      addMouseMotionListener(this);  
  25.      setBackground(new Color(128,128,255));  
  26.      setSize(350,350);  
  27.      show();  
  28.   }  
  29. /** 
  30.  *方法说明:绘制正方体盒子,过载Gr3d1m中的方法 
  31.  *输入参数: 
  32.  *返回类型: 
  33.  */  
  34.   public void drawPG(Graphics g,double []x,double []y,   
  35.                      double []z,int xp,int yp,Color co) {   
  36.      double x1,y1,z0;   
  37.      int len=x.length;  
  38.      double [] xw=new double[len];  
  39.      double [] yw=new double[len];  
  40.      int    [] xx=new int   [len];   
  41.      int    [] yy=new int   [len];   
  42.      final double RAD=Math.PI/180.0;  
  43.      double a=angX*RAD;   
  44.      double b=angY*RAD;  
  45.      double sinA=Math.sin(a),sinB=Math.sin(b);  
  46.      double cosA=Math.cos(a),cosB=Math.cos(b);  
  47.      for (int i=0; i<len; i++) {   
  48.         x1= x[i]*cosB+z[i]*sinB;  
  49.         z0=-x[i]*sinB+z[i]*cosB;   
  50.         y1= y[i]*cosA-  z0*sinA;  
  51.         xx[i]=xp+(int)Math.rint(x1);  
  52.         yy[i]=yp-(int)Math.rint(y1);  
  53.         xw[i]=x1; yw[i]=y1;   
  54.      }   
  55.      if (Hvec(xw,yw) > 0) {  
  56.         g.setColor(co);  
  57.         g.fillPolygon(xx,yy,len);//填充的多边形  
  58.      }   
  59.   }  
  60. /** 
  61.  *方法说明:消影处理,如果平面被遮蔽将不被绘制 
  62.  *输入参数: 
  63.  *返回类型: 
  64.  */  
  65.   double Hvec(double []x,double []y) {   
  66.     return(x[0]*(y[1]-y[2])+x[1]*(y[2]-y[0])+x[2]*(y[0]-y[1]));   
  67.   }  
  68.  }  
[java]  view plain  copy
  1. package test32;  
  2.   
  3. import java.util.*;  
  4. import java.io.*;  
  5. /** 
  6.  * Title: 标注输入输出 
  7.  * Description: 接收标准的键盘输入,和输出到屏幕。 
  8.  * Filename: standerdIO.java 
  9.  */  
  10. public class standerdIO{  
  11. /** 
  12.  *方法说明:主方法 
  13.  *输入参数: 
  14.  *返回类型: 
  15.  */  
  16.  public static void main(String[] args){  
  17.    Vector<Object> vTemp = new Vector<Object>();  
  18.    boolean flag = true;  
  19.    while(flag){  
  20.      System.out.print("input>");  
  21.      String sTemp ="";    
  22.      //读取输入,System.in表示接收键盘输入流  
  23.      BufferedReader stdin  = new BufferedReader(new InputStreamReader(System.in));  
  24.      try{  
  25.      //读取一行输入  
  26.       sTemp = stdin.readLine();  
  27.      }catch(IOException ie){  
  28.        System.err.println("IO error!");  
  29.      }  
  30.      //解析输入命令  
  31.      String sCMD="";  
  32.      String sContext="";  
  33.      int point = sTemp.indexOf(":");  
  34.      if(point==-1){  
  35.          sCMD = sTemp.trim();  
  36.      }else{  
  37.        sCMD = sTemp.substring(0,point);  
  38.        sContext = sTemp.substring(point+1);  
  39.      }  
  40.      //添加数据  
  41.      if(sCMD.equalsIgnoreCase("in")){  
  42.        if(sContext.equals("")){  
  43.          System.err.println("this command format is errer!");  
  44.        }else{  
  45.          vTemp.addElement(sContext);  
  46.        }     
  47.      }//查看结果  
  48.      else if(sCMD.equalsIgnoreCase("out")){  
  49.        for(int i=0;i<vTemp.size();i++){  
  50.          System.out.println(i+":"+vTemp.elementAt(i));  
  51.        }  
  52.      }//结束  
  53.      else if(sCMD.equalsIgnoreCase("quit")){  
  54.        flag=false;  
  55.      }  
  56.      else{  
  57.        System.err.println("this command don't run!");  
  58.        System.out.print("use:   in:command");  
  59.        System.out.print("use:   out");  
  60.      }  
  61.    }  
  62.  }  
  63. }  

[java]  view plain  copy
  1. package test33;  
  2.   
  3. import java.io.*;  
  4. /** 
  5.  * Title: 读取和写入文件 
  6.  * Description: 使用字节流方式操作文件,读取和写入文件。 
  7.  * Filename: CopyBytes.java 
  8.  */  
  9. public class BAKCopyBytes {  
  10. /** 
  11.  *方法说明:主方法 
  12.  *输入参数: 
  13.  *返回类型: 
  14.  */  
  15.     public static void main(String[] args) throws IOException {  
  16.         String sFile;  
  17.         String oFile;  
  18.         if(args.length<2){  
  19.           System.out.println("USE:java CopyBytes source file | object file");  
  20.           return;  
  21.         }else{  
  22.           sFile = args[0];  
  23.           oFile = args[1];  
  24.         }  
  25.         try{  
  26.           File inputFile = new File(sFile);//定义读取源文件  
  27.           File outputFile = new File(oFile);//定义拷贝目标文件  
  28.           //定义输入文件流  
  29.           FileInputStream in = new FileInputStream(inputFile);  
  30.           //将文件输入流构造到缓存  
  31.           BufferedInputStream bin = new BufferedInputStream(in);  
  32.           //定义输出文件流  
  33.           FileOutputStream out = new FileOutputStream(outputFile);  
  34.           //将输出文件流构造到缓存  
  35.           BufferedOutputStream bout = new BufferedOutputStream(out);  
  36.           int c;  
  37.           //循环读取文件和写入文件  
  38.           while ((c = bin.read()) != -1)  
  39.              bout.write(c);  
  40.           //关闭输入输出流,释放资源  
  41.           bin.close();  
  42.           bout.close();  
  43.         }catch(IOException e){  
  44.           //文件操作,捕获IO异常。  
  45.           System.err.println(e);  
  46.         }  
  47.     }  
  48. }  

[java]  view plain  copy
  1. package test33;  
  2.   
  3. import java.io.*;  
  4. /** 
  5.  * Title: 读取和写入文件 
  6.  * Description: 使用字节流方式操作文件,读取和写入文件。 
  7.  * Filename: CopyBytes.java 
  8.  */  
  9. public class CopyBytes {  
  10. /** 
  11.  *方法说明:主方法 
  12.  *输入参数: 
  13.  *返回类型: 
  14.  */  
  15.     public static void main(String[] args) throws IOException {  
  16.         String sFile;  
  17.         String oFile;  
  18.         if(args.length<2){  
  19.           System.out.println("USE:java CopyBytes source file | object file");  
  20.           return;  
  21.         }else{  
  22.           sFile = args[0];  
  23.           oFile = args[1];  
  24.         }  
  25.         try{  
  26.           File inputFile = new File(sFile);//定义读取源文件  
  27.           File outputFile = new File(oFile);//定义拷贝目标文件  
  28.           //定义输入文件流  
  29.           FileInputStream in = new FileInputStream(inputFile);  
  30.           //将文件输入流构造到缓存  
  31.           BufferedInputStream bin = new BufferedInputStream(in);  
  32.           //定义输出文件流  
  33.           FileOutputStream out = new FileOutputStream(outputFile);  
  34.           //将输出文件流构造到缓存  
  35.           BufferedOutputStream bout = new BufferedOutputStream(out);  
  36.           int c;  
  37.           //循环读取文件和写入文件  
  38.           while ((c = bin.read()) != -1)  
  39.              bout.write(c);  
  40.           //关闭输入输出流,释放资源  
  41.           bin.close();  
  42.           bout.close();  
  43.         }catch(IOException e){  
  44.           //文件操作,捕获IO异常。  
  45.           System.err.println(e);  
  46.         }  
  47.     }  
  48. }  

[java]  view plain  copy
  1. package test34;  
  2.   
  3. import java.io.*;  
  4.   
  5. /** 
  6.  * Title: 文件的读取和写入(字符) 
  7.  * Description: 使用FileReader和FileWriter类,采用字符文件访问方式操作文件。 
  8.  * Filename:  
  9.  */  
  10. public class BAKCopyChar {  
  11. /** 
  12.  *方法说明:主方法 
  13.  *输入参数: 
  14.  *返回类型: 
  15.  */  
  16.     public static void main(String[] args) throws IOException {  
  17.         String sFile;  
  18.         String oFile;  
  19.         if(args.length<2){  
  20.           System.out.println("USE:java CopyChar source file | object file");  
  21.           return;  
  22.         }else{  
  23.           sFile = args[0];  
  24.           oFile = args[1];  
  25.         }  
  26.         try{  
  27.           File inputFile   = new File(sFile);//定义读取的文件源  
  28.           File outputFile = new File(oFile);//定义拷贝的目标文件  
  29.           //定义输入文件流  
  30.           FileReader in   = new FileReader(inputFile);  
  31.           //将文件输入流构造到缓存  
  32.           BufferedReader bin = new BufferedReader(in);  
  33.           //定义输出文件流  
  34.           FileWriter out  = new FileWriter(outputFile);  
  35.           //将输出文件流构造到缓存  
  36.           BufferedWriter bout = new BufferedWriter(out);  
  37.           int c;  
  38.           //循环读取和输入文件。  
  39.           while ((c = bin.read()) != -1)  
  40.              bout.write(c);  
  41.           bin.close();  
  42.           bout.close();  
  43.         }catch(IOException e){  
  44.           //文件操作,捕获IO异常。  
  45.           System.err.println(e);  
  46.         }  
  47.     }  
  48. }  

[java]  view plain  copy
  1. package test34;  
  2.   
  3. import java.io.*;  
  4.   
  5. /** 
  6.  * Title: 文件的读取和写入(字符) 
  7.  * Description: 使用FileReader和FileWriter类,采用字符文件访问方式操作文件。 
  8.  * Filename:  
  9.  */  
  10. public class CopyChar {  
  11. /** 
  12.  *方法说明:主方法 
  13.  *输入参数: 
  14.  *返回类型: 
  15.  */  
  16.     public static void main(String[] args) throws IOException {  
  17.         String sFile;  
  18.         String oFile;  
  19.         if(args.length<2){  
  20.           System.out.println("USE:java CopyChar source file | object file");  
  21.           return;  
  22.         }else{  
  23.           sFile = args[0];  
  24.           oFile = args[1];  
  25.         }  
  26.         try{  
  27.           File inputFile   = new File(sFile);//定义读取的文件源  
  28.           File outputFile = new File(oFile);//定义拷贝的目标文件  
  29.           //定义输入文件流  
  30.           FileReader in   = new FileReader(inputFile);  
  31.           //将文件输入流构造到缓存  
  32.           BufferedReader bin = new BufferedReader(in);  
  33.           //定义输出文件流  
  34.           FileWriter out  = new FileWriter(outputFile);  
  35.           //将输出文件流构造到缓存  
  36.           BufferedWriter bout = new BufferedWriter(out);  
  37.           int c;  
  38.           //循环读取和输入文件。  
  39.           while ((c = bin.read()) != -1)  
  40.              bout.write(c);  
  41.           bin.close();  
  42.           bout.close();  
  43.         }catch(IOException e){  
  44.           //文件操作,捕获IO异常。  
  45.           System.err.println(e);  
  46.         }  
  47.     }  
  48. }  

[java]  view plain  copy
  1. package test35;  
  2.   
  3. import java.io.*;  
  4. import java.util.*;  
  5. /** 
  6.  * Title: 文件操作 
  7.  * Description: 演示文件的删除和获取文件的信息 
  8.  * Filename:  
  9.  */  
  10. public class fileOperation{  
  11. /** 
  12.  *方法说明:删除文件 
  13.  *输入参数:String fileName 要删除的文件名 
  14.  *返回类型:boolean 成功为true 
  15.  */  
  16.   public boolean delFile(String fileName){  
  17.     try{  
  18.       //删除文件  
  19.       boolean success = (new File(fileName)).delete();  
  20.       if (!success) {  
  21.          System.out.println("delete file error!");  
  22.          return false;  
  23.       }else{  
  24.          return true;  
  25.       }  
  26.     }catch(Exception e){  
  27.       System.out.println(e);  
  28.       return false;  
  29.     }  
  30.   }  
  31. /** 
  32.  *方法说明:获取文件信息 
  33.  *输入参数:String Name 文件名 
  34.  *返回类型:String[] 文件信息数组 
  35.  */  
  36.   public String[] getFileInfo(String Name){  
  37.     try{  
  38.       File file = new File(Name);  
  39.       //获取文件修改日期(返回的是句)  
  40.       long modifiedTime = file.lastModified();  
  41.       //获取文件长度(单位:Bite)  
  42.       long filesize = file.length();  
  43.       //测试文件是否可读  
  44.       boolean cr = file.canRead();  
  45.       //测试文件是否可写  
  46.       boolean cw = file.canWrite();  
  47.       //测试文件是否隐藏  
  48.       boolean ih = file.isHidden();  
  49.         
  50.       String[] sTemp = new String[6];  
  51.       sTemp[0] = String.valueOf(filesize);  
  52.       sTemp[1] = getDateString(modifiedTime);  
  53.       sTemp[2] = String.valueOf(cr);  
  54.       sTemp[3] = String.valueOf(cw);  
  55.       sTemp[4] = String.valueOf(ih);  
  56.       sTemp[5] = String.valueOf(file.getCanonicalPath());  
  57.       return sTemp;  
  58.     }catch(Exception e){  
  59.       System.out.println(e);  
  60.       return null;  
  61.     }  
  62.   }  
  63.   
  64. /** 
  65.  *方法说明:将毫秒数字转换为日期 
  66.  *输入参数:mill    毫秒数 
  67.  *返回类型:String 字符 格式为:yyyy-mm-dd hh:mm 
  68.  */  
  69.   public static String getDateString(long mill)  
  70.   {  
  71.     if(mill < 0return  "";  
  72.       
  73.     Date date = new Date(mill);  
  74.     Calendar rightNow = Calendar.getInstance();  
  75.     rightNow.setTime(date);  
  76.     int year = rightNow.get(Calendar.YEAR);  
  77.     int month = rightNow.get(Calendar.MONTH);  
  78.     int day = rightNow.get(Calendar.DAY_OF_MONTH);  
  79.     int hour = rightNow.get(Calendar.HOUR_OF_DAY);  
  80.     int min = rightNow.get(Calendar.MINUTE);  
  81.   
  82.     return year + "-" + (month <10 ? "0" + month : "" + month) + "-"   
  83.            +  (day <10 ? "0" + day : "" + day)  
  84.            +  (hour <10 ? "0" + hour : "" + hour)+":"  
  85.            + (min <10 ? "0" + min : "" + min);  
  86.   }  
  87. /** 
  88.  *方法说明:主方法 
  89.  *输入参数: 
  90.  *返回类型: 
  91.  */  
  92.   public static void main(String[] args){  
  93.     try{  
  94.       fileOperation fo = new fileOperation();  
  95.       if(args.length==0){  
  96.         return;  
  97.       }else{  
  98.         String cmd = args[0];  
  99.         if(cmd.equals("del")){  
  100.           boolean bdel = fo.delFile(args[1]);  
  101.           System.out.println(bdel);  
  102.         }else if(cmd.equals("info")){  
  103.           String[] sTemp = fo.getFileInfo(args[1]);  
  104.           for(int i=0;i<sTemp.length;i++)  
  105.             System.out.println(sTemp[i]);  
  106.         }  
  107.         
  108.       }  
  109.     }catch(Exception e){  
  110.       return;  
  111.     }  
  112.   }  
  113. }  

[java]  view plain  copy
  1. package test36;  
  2.   
  3. /** 
  4.  * Title: 目录操作 
  5.  * Description: 演示列目录下的文件,和移动一个目录 
  6.  * Filename: Dir.java 
  7.  */  
  8. import java.io.*;  
  9. public class Dir{  
  10.  /** 
  11.  *方法说明:实现目录列表 
  12.  *输入参数: 
  13.  *返回类型: 
  14.  */   
  15.   public String[] DirList(String pathName){  
  16.     try{  
  17.       File path = null;  
  18.       String[] fileList;  
  19.       //如果没有指定目录,则列出当前目录。  
  20.       if(pathName.equals(""))  
  21.         path = new File(".");  
  22.       else  
  23.         path = new File(pathName);  
  24.       //获取目录文件列表  
  25.       if(path.isDirectory())  
  26.         fileList = path.list();  
  27.       else  
  28.         return null;  
  29.      return fileList;  
  30.     }catch(Exception e){  
  31.       System.err.println(e);  
  32.       return null;  
  33.     }  
  34.   }  
  35. /** 
  36.  *方法说明:移动一个目录 
  37.  *输入参数:String sou 源目录, String obj 新目录 
  38.  *返回类型: 
  39.  */  
  40.   public boolean DirMove(String sou, String obj){  
  41.     try{  
  42.      //检查源文件是否存在  
  43.       boolean sexists = (new File(sou)).isDirectory();  
  44.       if(!sexists) return false;  
  45.       boolean oexists = (new File(obj)).isDirectory();  
  46.       //目标目录不存在,建立一个  
  47.       if(!oexists){  
  48.         (new File(obj)).mkdirs();  
  49.       }  
  50.      
  51.         File file = new File(sou);  
  52.         File dir = new File(obj);  
  53.         //移动目录  
  54.         boolean success = file.renameTo(new File(dir, file.getName()));  
  55.         if (!success) {  
  56.          System.out.println("copy error!");  
  57.          return false;  
  58.         }  
  59.         else return true;  
  60.     }catch(Exception e){  
  61.         System.out.println(e);  
  62.         return false;  
  63.     }  
  64.       
  65.   }  
  66.   
  67. /** 
  68.  *方法说明:主方法 
  69.  *输入参数: 
  70.  *返回类型: 
  71.  */  
  72.   public static void main(String[] args){  
  73.      Dir d = new Dir();  
  74.     if(args.length==0){  
  75.       return;  
  76.     }else{  
  77.       String cmd = args[0];  
  78.       if(cmd.equals("list")){  
  79.         if(args.length!=2return;  
  80.         String[] sTemp = d.DirList(args[1]);  
  81.         for(int i=0;i<sTemp.length;i++)  
  82.           System.out.println(sTemp[i]);  
  83.       }else if(cmd.equals("move")){  
  84.         if(args.length!=3return;            
  85.         d.DirMove(args[1],args[2]);  
  86.       }  
  87.         
  88.     }  
  89.    }  
  90. }   

[java]  view plain  copy
  1. package test37;  
  2.   
  3. import java.io.*;  
  4. /** 
  5.  * Title: 读取随机文件 
  6.  * Description: 演示使用RandomAccessFile类读取文件。 
  7.  * Filename: RandFile.java 
  8.  */  
  9. public class RandFile{  
  10. /** 
  11.  *方法说明:主方法 
  12.  *输入参数: 
  13.  *返回类型: 
  14.  */  
  15.   public static void main(String[] args){  
  16.     String sFile;  
  17.     if(args.length<1){  
  18.       System.out.println("USE:java RandFile fileName");  
  19.       return;  
  20.     }else{  
  21.       sFile = args[0];  
  22.     }  
  23.     //接受IOException异常  
  24.     try{  
  25.       //构造随机访问文件,使用可读写方式。  
  26.       RandomAccessFile rf = new  RandomAccessFile(sFile, "rw");  
  27.       for(int i = 0; i < 10; i++)  
  28.         rf.writeDouble(i*1.414);  
  29.       rf.close();  
  30.       //构造一个随机访问文件,使用只读方式  
  31.       rf = new RandomAccessFile(sFile, "rw");  
  32.       rf.seek(5*8);  
  33.       rf.writeDouble(47.0001);  
  34.       rf.close();  
  35.       //构造一个随机文件访问文件,使用只读方式。  
  36.       rf = new RandomAccessFile(sFile, "r");  
  37.       for(int i = 0; i < 10; i++)  
  38.         System.out.println("Value " + i + ": " + rf.readDouble());  
  39.       rf.close();  
  40.      }catch(IOException e){  
  41.        System.out.println(e);  
  42.      }  
  43.   }  
  44. }  

[java]  view plain  copy
  1. package test38;  
  2.   
  3. import java.io.File;   
  4. import jxl.*;  
  5. import jxl.write.*;   
  6. /** 
  7.  * Title: 操作EXCEL文件 
  8.  * Description: 本实例演示使用jxl包实现对excel文件的操作 
  9.  * Filename: myExcel.java 
  10.  */  
  11. public class myExcel{  
  12.   Workbook workbook;  
  13.   Sheet sheet;  
  14. /** 
  15.  *方法说明:写入文件操作 
  16.  *输入参数: 
  17.  *返回类型: 
  18.  */  
  19.   public void write(){  
  20.     try{  
  21.         //创建一个可写入的excel文件对象  
  22.         WritableWorkbook workbook = Workbook.createWorkbook(new File("myfile.xls"));   
  23.         //使用第一张工作表,将其命名为“午餐记录”  
  24.         WritableSheet sheet = workbook.createSheet("午餐记录"0);   
  25.         //表头  
  26.         Label label0 = new Label(00"时间");   
  27.         sheet.addCell(label0);   
  28.         Label label1 = new Label(10"姓名");   
  29.         sheet.addCell(label1);   
  30.         Label label2 = new Label(20"午餐标准");   
  31.         sheet.addCell(label2);   
  32.         Label label3 = new Label(30"实际费用");   
  33.         sheet.addCell(label3);   
  34.         //格式化日期  
  35.         jxl.write.DateFormat df = new jxl.write.DateFormat("yyyy-dd-MM  hh:mm:ss");   
  36.         jxl.write.WritableCellFormat wcfDF = new jxl.write.WritableCellFormat(df);   
  37.         jxl.write.DateTime labelDTF = new jxl.write.DateTime(01new java.util.Date(), wcfDF);   
  38.         sheet.addCell(labelDTF);  
  39.         //普通字符  
  40.         Label labelCFC = new Label(11"riverwind");   
  41.         sheet.addCell(labelCFC);   
  42.          //格式化数字  
  43.         jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");   
  44.         WritableCellFormat wcfN = new WritableCellFormat(nf);   
  45.         jxl.write.Number labelNF = new jxl.write.Number(2113.1415926, wcfN);   
  46.         sheet.addCell(labelNF);   
  47.           
  48.            
  49.         jxl.write.Number labelNNF = new jxl.write.Number(3110.50001, wcfN);   
  50.         sheet.addCell(labelNNF);   
  51.         //关闭对象,释放资源  
  52.         workbook.write();   
  53.         workbook.close();   
  54.   
  55.     }catch(Exception e){  
  56.       System.out.println(e);  
  57.     }  
  58.   }  
  59. /** 
  60.  *方法说明:读取excel文件一行数据 
  61.  *输入参数:int row指定的行数 
  62.  *返回类型:String〔〕结果数组 
  63.  */    
  64.   public String[] readLine(int row){  
  65.     try{  
  66.       //获取数据表列数  
  67.       int colnum = sheet.getColumns();  
  68.       String[] rest = new String[colnum];  
  69.       for(int i = 0; i < colnum; i++){  
  70.         String sTemp = read(i,row);  
  71.         if(sTemp!=null)  
  72.          rest[i] = sTemp;  
  73.       }  
  74.       return rest;  
  75.     }catch(Exception e){  
  76.       System.out.println("readLine err:"+e);  
  77.       workbook.close();  
  78.       return null;  
  79.     }  
  80.   }  
  81. /** 
  82.  *方法说明:读取excel的指定单元数据 
  83.  *输入参数: 
  84.  *返回类型: 
  85.  */  
  86.   public String read(int col, int row){  
  87.     try{  
  88.       //获得单元数据  
  89.       Cell a2 = sheet.getCell(col,row);   
  90.       String rest = a2.getContents();  
  91.       return rest;  
  92.     }catch(Exception e){  
  93.       System.out.println("read err:"+e);  
  94.       workbook.close();  
  95.       return null;  
  96.     }  
  97.   }  
  98. /** 
  99.  *方法说明:主方法,演示程序用 
  100.  *输入参数: 
  101.  *返回类型: 
  102.  */  
  103.   public static void main(String[] arges){  
  104.     try{  
  105.       myExcel me = new myExcel();  
  106.       //生成一个可读取的excel文件对象  
  107.       me.workbook = Workbook.getWorkbook(new File("myfile.xls"));  
  108.       //使用第一个工作表  
  109.       me.sheet = me.workbook.getSheet(0);  
  110.       //读一行记录,并显示出来  
  111.       String[] ssTemp = me.readLine(1);  
  112.       for(int i=0;i<ssTemp.length;i++)  
  113.        System.out.println(ssTemp[i]);  
  114.       //写入数据  
  115.       me.write();  
  116.         
  117.       me.workbook.close();  
  118.     }catch(Exception e){  
  119.       System.out.println(e);  
  120.     }  
  121.   }  
  122.      
  123. }  

[java]  view plain  copy
  1. package test39;  
  2.   
  3. import com.lowagie.text.*;  
  4. import com.lowagie.text.pdf.*;  
  5. import java.io.*;  
  6. import java.awt.Color;  
  7.   
  8. /** 
  9.  * Title: 生成PDF文件 
  10.  * Description: 本实例通过使用iText包生成一个表格的PDF文件 
  11.  * Filename: myPDF.java 
  12.  */  
  13. public class myPDF{  
  14. /** 
  15.  *方法说明:写PDF文件 
  16.  *输入参数: 
  17.  *返回类型: 
  18.  */  
  19.   public void write(){  
  20.    try{  
  21.      Document document=new Document(PageSize.A4, 505010050);  
  22.      Rectangle pageRect=document.getPageSize();  
  23.      PdfWriter.getInstance(document, new FileOutputStream("tables.pdf"));  
  24.      //创建汉字字体  
  25.      BaseFont bfSong = BaseFont.createFont("STSong-Light""UniGB-UCS2-H"false);  
  26.      Font fontSong = new Font(bfSong, 10, Font.NORMAL);  
  27.      // 增加一个水印  
  28.      try {  
  29.          Watermark watermark = new Watermark(Image.getInstance("test.jpg"), pageRect.left()+50,pageRect.top()-85);  
  30.          watermark.scalePercent(50);  
  31.          document.add(watermark);  
  32.      }catch(Exception e) {  
  33.           System.err.println("请查看文件“test.jpg”是否在正确的位置?");  
  34.      }  
  35.        
  36.       // 为页增加页头信息  
  37.      HeaderFooter header = new HeaderFooter(new Phrase("Java实例一百例",fontSong), false);  
  38.      header.setBorder(2);  
  39.      header.setAlignment(Element.ALIGN_RIGHT);  
  40.      document.setHeader(header);  
  41.        
  42.       // 为页增加页脚信息  
  43.      HeaderFooter footer = new HeaderFooter(new Phrase("第 ",fontSong),new Phrase(" 页",fontSong));  
  44.      footer.setAlignment(Element.ALIGN_CENTER);  
  45.      footer.setBorder(1);  
  46.      document.setFooter(footer);  
  47.   
  48.       // 打开文档  
  49.      document.open();   
  50.      //构造表格  
  51.      Table table = new Table(4);  
  52.      table.setDefaultVerticalAlignment(Element.ALIGN_MIDDLE);  
  53.      table.setBorder(Rectangle.NO_BORDER);  
  54.      int hws[] = {10201020,};  
  55.      table.setWidths(hws);  
  56.      table.setWidth(100);  
  57.      //表头信息  
  58.      Cell cellmain = new Cell(new Phrase("用户信息",new Font(bfSong, 10, Font.BOLD,new Color(0,0,255))));  
  59.      cellmain.setHorizontalAlignment(Element.ALIGN_CENTER);  
  60.      cellmain.setColspan(4);  
  61.      cellmain.setBorder(Rectangle.NO_BORDER);  
  62.      cellmain.setBackgroundColor(new Color(0xC00xC00xC0));  
  63.      table.addCell(cellmain);  
  64.       //分表头信息  
  65.      Cell cellleft= new Cell(new Phrase("收货人信息",new Font(bfSong, 10, Font.ITALIC,new Color(0,0,255))));  
  66.      cellleft.setColspan(2);  
  67.      cellleft.setHorizontalAlignment(Element.ALIGN_CENTER);  
  68.      table.addCell(cellleft);  
  69.      Cell cellright= new Cell(new Phrase("订货人信息",new Font(bfSong, 10, Font.ITALIC,new Color(0,0,255))));  
  70.      cellright.setColspan(2);  
  71.      cellright.setHorizontalAlignment(Element.ALIGN_CENTER);  
  72.      table.addCell(cellright);  
  73.        
  74.      //收货和订货人信息,表体内容  
  75.      table.addCell(new Phrase("姓名",fontSong));  
  76.      table.addCell(new Phrase("张三",fontSong));  
  77.      table.addCell(new Phrase("姓名",fontSong));  
  78.      table.addCell(new Phrase("李四",fontSong));  
  79.   
  80.      table.addCell(new Phrase("电话",fontSong));  
  81.      table.addCell(new Phrase("23456789",fontSong));  
  82.      table.addCell(new Phrase("电话",fontSong));  
  83.      table.addCell(new Phrase("9876543",fontSong));  
  84.   
  85.      table.addCell(new Phrase("邮编",fontSong));  
  86.      table.addCell(new Phrase("100002",fontSong));  
  87.      table.addCell(new Phrase("邮编",fontSong));  
  88.      table.addCell(new Phrase("200001",fontSong));  
  89.   
  90.      table.addCell(new Phrase("地址",fontSong));  
  91.      table.addCell(new Phrase("北京西城区XX路XX号",fontSong));  
  92.      table.addCell(new Phrase("地址",fontSong));  
  93.      table.addCell(new Phrase("上海陆家嘴区XX路XX号",fontSong));  
  94.   
  95.      table.addCell(new Phrase("电子邮件",fontSong));  
  96.      table.addCell(new Phrase("zh_san@hotmail.com",fontSong));  
  97.      table.addCell(new Phrase("电子邮件",fontSong));  
  98.      table.addCell(new Phrase("li_si@hotmail.com",fontSong));  
  99.      //将表格添加到文本中  
  100.      document.add(table);  
  101.      //关闭文本,释放资源  
  102.      document.close();   
  103.        
  104.    }catch(Exception e){  
  105.      System.out.println(e);     
  106.    }  
  107.   }  
  108. /** 
  109.  *方法说明:主方法 
  110.  *输入参数: 
  111.  *返回类型: 
  112.  */  
  113.   public static void main(String[] arg){  
  114.     myPDF p = new myPDF();  
  115.     p.write();  
  116.   }  
  117. }  

[java]  view plain  copy
  1. package test40;  
  2.   
  3. //文件名:myZip.java  
  4. import java.io.*;  
  5. import java.util.*;  
  6. import java.util.zip.*;  
  7. /** 
  8.  * Title: 文件压缩和解压 
  9.  * Description: 使用ZipInputStream和ZipOutputStream对文件 
  10.  *                 和目录进行压缩和解压处理 
  11.  * Filename: myZip.java 
  12.  */  
  13. public class myZip{  
  14. /** 
  15.  *方法说明:实现文件的压缩处理 
  16.  *输入参数:String[] fs 压缩的文件数组 
  17.  *返回类型: 
  18.  */  
  19.   public void ZipFiles(String[] fs){  
  20.    try{  
  21.      String fileName = fs[0];  
  22.      FileOutputStream f =  
  23.        new FileOutputStream(fileName+".zip");  
  24.      //使用输出流检查  
  25.      CheckedOutputStream cs =   
  26.         new CheckedOutputStream(f,new Adler32());  
  27.       //声明输出zip流  
  28.       ZipOutputStream out =  
  29.         new ZipOutputStream(new BufferedOutputStream(cs));  
  30.       //写一个注释  
  31.       out.setComment("A test of Java Zipping");  
  32.       //对多文件进行压缩  
  33.       for(int i=1;i<fs.length;i++){  
  34.         System.out.println("Write file "+fs[i]);  
  35.         BufferedReader in =  
  36.            new BufferedReader(  
  37.              new FileReader(fs[i]));  
  38.          out.putNextEntry(new ZipEntry(fs[i]));  
  39.          int c;  
  40.          while((c=in.read())!=-1)  
  41.           out.write(c);  
  42.         in.close();  
  43.        }  
  44.        //关闭输出流  
  45.        out.close();  
  46.        System.out.println("Checksum::"+cs.getChecksum().getValue());  
  47.     }catch(Exception e){  
  48.        System.err.println(e);  
  49.     }  
  50.   }  
  51.   
  52. /** 
  53.  *方法说明:解压缩Zip文件 
  54.  *输入参数:String fileName 解压zip文件名 
  55.  *返回类型: 
  56.  */  
  57.   public void unZipFile(String fileName){  
  58.     try{  
  59.        System.out.println("读取ZIP文件........");  
  60.        //文件输入流  
  61.        FileInputStream fi =  
  62.          new FileInputStream(fileName+".zip");  
  63.        //输入流检查  
  64.        CheckedInputStream csi = new CheckedInputStream(fi,new Adler32());  
  65.        //输入流压缩  
  66.        ZipInputStream in2 =  
  67.          new ZipInputStream(  
  68.            new BufferedInputStream(csi));  
  69.        ZipEntry ze;  
  70.        System.out.println("Checksum::"+csi.getChecksum().getValue());  
  71.        //解压全部文件  
  72.        while((ze = in2.getNextEntry())!=null){  
  73.          System.out.println("Reading file "+ze);  
  74.          int x;  
  75.          while((x= in2.read())!=-1)  
  76.            //这里是写文件,write是以byte方式输出。  
  77.            System.out.write(x);  
  78.        }  
  79.        in2.close();  
  80.     }catch(Exception e){  
  81.       System.err.println(e);  
  82.     }  
  83.   }  
  84. /** 
  85.  *方法说明:读取Zip文件列表 
  86.  *输入参数:String fileName zip文件名 
  87.  *返回类型:Vector 文件列表 
  88.  */  
  89.   @SuppressWarnings("unchecked")  
  90. public Vector<Object> listFile(String fileName){  
  91.     try{  
  92.        @SuppressWarnings("unused")  
  93.     String[] aRst=null;  
  94.        Vector<Object> vTemp = new Vector<Object>();  
  95.        //zip文件对象  
  96.        ZipFile zf = new ZipFile(fileName+".zip");  
  97.        Enumeration e = zf.entries();  
  98.        while(e.hasMoreElements()){  
  99.          ZipEntry ze2 = (ZipEntry)e.nextElement();  
  100.          System.out.println("File: "+ze2);  
  101.          vTemp.addElement(ze2);  
  102.        }  
  103.        return  vTemp;  
  104.     }catch(Exception e){  
  105.       System.err.println(e);  
  106.       return null;  
  107.     }  
  108.   }  
  109. /** 
  110.  *方法说明:主方法 
  111.  *输入参数: 
  112.  *返回类型: 
  113.  */  
  114.   public static void main(String[] args){  
  115.     try{  
  116.      String fileName = args[0];  
  117.      myZip myZip = new myZip();  
  118.      myZip.ZipFiles(args);  
  119.      myZip.unZipFile(fileName);  
  120.      Vector<Object> dd = myZip.listFile(fileName);  
  121.      System.out.println("File List: "+dd);  
  122.     }catch(Exception e){  
  123.         e.printStackTrace();  
  124.     }  
  125.   }  
  126. }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值