管理 撰写
2007/03/27 @ 15:46
刚刚做了个类似功能,与大家分享下,大家多提意见:


import java.io.*;
import java.util.zip.*;
import java.util.*;

public class UnZip {
 static final int BUFFER = 2048;     //设置缓冲流
 public static String[] unZip(String path) {
     String[] list = null;
     String patht = new String("文件路径");
   try {
     BufferedOutputStream dest = null;
     FileInputStream fis = new FileInputStream(path);   //获得输入流
     ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
     ZipEntry entry;
     while ( (entry = zis.getNextEntry()) != null) {
       int count;
       byte data[] = new byte[BUFFER];    //开始读入
       // 分文件写到磁盘
       FileOutputStream fos = new FileOutputStream(patht+entry.getName());
       dest = new BufferedOutputStream(fos, BUFFER);
       while ( (count = zis.read(data, 0, BUFFER))
              != -1) {
         dest.write(data, 0, count);  
       }
       //关闭流
       dest.flush();
       dest.close();
     }
     zis.close();
     File f = new File(patht);
     list = f.list(); //返回由path指定路径里的所有文件名和目录名
   }
   catch (Exception e) {
     e.printStackTrace();
   }
   return list;
 }
}
2007/03/27 @ 15:45
首先,到 http://jakarta.apache.org/poi/ 下在 jakarta poi。

读excel:


import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileInputStream;
public class ReadXL {
 //
 public static String fileToBeRead="yourfile_dir";
 public static void main(String argv[]){
 try{
  // 创建对Excel工作簿文件的引用
  HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));
  HSSFSheet sheet = workbook.getSheet("Sheet");
  HSSFRow row = sheet.getRow(0);
  HSSFCell cell = row.getCell((short)0);
  System.out.println("left_upon"+ cell.getStringCellValue());
 }catch(Exception e) {
  ......................|
 }
}
}


写excel:

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileOutputStream;
public class CreateXL {

 //
 public static String outputFile="yourfile_dir";

 public static void main(String argv[]){

 try{

  // 创建新的Excel
  HSSFWorkbook workbook = new HSSFWorkbook();

  HSSFSheet sheet = workbook.createSheet();
  HSSFRow row = sheet.createRow((short)0);

  //在索引0的位置创建单元格
  HSSFCell cell = row.createCell((short) 0);
  cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  cell.setCellValue("增加值");
  FileOutputStream fOut = new FileOutputStream(outputFile);
  workbook.write(fOut);
  fOut.flush();
  // 操作结束,关闭文件
  fOut.close();
  System.out.println("execute success...");

 }catch(Exception e) {
  ...................
 }
}
}

仅供参考!

2007/03/27 @ 15:41
今天在网上逛的时候,看到一段识别java注释代码的自动机,所以跟大家分享!(因为链接地址不记得了,所以涉及版权问题请与我联系)


class CommentAutoMata{
   final int start=0;
   final int one=1;
   final int two=2;
   final int three=3;
   final int finalState=4;
   final int five=5;
   final int six=6;
   public List getCommentList(String s){
       ArrayList ret=new ArrayList();
       int begin=-1;      
       int curState=start;
       for(int i=0;i<s.length();i++){
           char c=s.charAt(i);
           switch(curState){
           case(start):
               if(c=='/'){
                   begin=i;
                   curState=one;
               }
               else if(c=='"'){
                   //识别字符出
                   curState=5;
               }
               break;
           case(one):
               if(c=='*') curState=two;
               else curState=start;            
               break;
           case(two):
               if(c=='*') curState=three;
               break;
           case(three):
               if(c=='/'){
                   curState=finalState;
                    //eat
                   ret.add(s.substring(begin,i+1));
                   //reset;
                   curState=start;
                   begin=-1;
               }
               else curState=two;
               break;
           case(five):
               if(c=='"') curState=start;
               else if(c=='\\')curState=six;
               break;
           case(six):
               curState=five;
               break;
             
           }
       }
       
       return ret;
   }
}
分页: 13/15 第一页 上页 8 9 10 11 12 13 14 15 下页 最后页 [ 显示模式: 摘要 | 列表 ]