星期三, 十月 24, 2007

用java操纵文件后缀名

 import java.io.File;

 /*
  * Created on 2006-9-19
  *
  * TODO To change the template for this generated file go to
  * Window - Preferences - Java - Code Style - Code Templates
  */

 /**
  * @author public2298
  *
  * TODO To change the template for this generated type comment go to
  * Window - Preferences - Java - Code Style - Code Templates
  */
 public abstract class BaseFolder {
  private String folderPath = null;
  protected String suffix = null;
  
  public void visit(File file)
  {
   if(file.isDirectory())
   {
       if(getCondition(file))
           folderOper(file);
       else
       {
     File[] fileArray = file.listFiles();
     for(int i=0;i<fileArray.length;i++)
     {
      visit(fileArray[i]);
     }
       }
   }
   else
   {
    fileOper(file);
   }
  }
  protected void folderOper(File file){}
  
  protected void fileOper(File file){}
  protected boolean getCondition(File file){return false;}
  protected void deleteFolder(File file)
  {
   if(file.isDirectory())
   {
    File[] fileArray = file.listFiles();
    for(int i=0;i<fileArray.length;i++)
    {
     deleteFolder(fileArray[i]);
    }
    file.delete();
   }
   else
    file.delete();
  }
  //添加后缀名
  protected void addSuffix(File file)
  {
      formatSuffix();
      file.renameTo(new File(file.getAbsolutePath()+suffix));
  }
  //去掉后缀名
  protected void deleteSuffix(File file)
  {
      formatSuffix();
      String path = file.getAbsolutePath();
      if(path.endsWith(suffix))
      {
          path = path.substring(0,path.length()-suffix.length());
          file.renameTo (new File(path));
      }
  }
  //如果后缀名不含有点,就加上点.
  private void formatSuffix()
  {
      if((suffix!=null)&&(!suffix.startsWith(".")))
          suffix = "."+suffix;
  }
  protected void clean()
  {
   
  }
  
  protected void setSuffix(String suffix) {
         this.suffix = suffix;
     }
 }

 
 //工具类

 import java.io.File;

 /*
  * Created on 2006-9-27

  * Window - Preferences - Java - Code Style - Code Templates
  */

 public class FolderUtils {
     private File file;
     private BaseFolder folder;
     public FolderUtils(File file)
     {
         this.file = file;
     }
     
     public void addSuffix(String suffix)
     {
         folder = new AddFileSuffix();
         folder.setSuffix(suffix);
         folder.visit(file);     
     }
     public void deleteSuffix(String suffix)
     {
         folder = new DeleteFileSuffix();
         folder.setSuffix(suffix);
         folder.visit(file);     
     }
     public void deleteFileByPattern(String pattern)
     {
         folder = new DeleteFileByPattern(pattern);
         folder.visit (file);
     }
     public void deleteFileBySuffix(String suffix)
     {
         folder = new DeleteFileBySuffix(suffix);
         folder.visit(file);
     }
     public void deleteFodler(String pattern)
     {
         folder = new DeleteFolderByName(pattern);
         folder.visit(file);
     }
     public void renameFile(long startNumber)
     {
         folder = new RenameFileLikeNumber(startNumber);
         folder.visit(file);
     }
     public static void main(String[] args) {
         FolderUtils util = new FolderUtils(new File("d:/src/yy"));
         //util.addSuffix(".yx");
         //util.deleteSuffix(".yx");
         //util.deleteFileByPattern("yinxing.txt");
         //util.deleteFileBySuffix(".yx");
         //util.deleteFodler("yyy");
         util.renameFile(999);
     }
     

     public void setFile(File file) {
         this.file = file;
     }
 }

 //如果文件是以指定名字为后缀,就删除文件的后缀
 class DeleteFileSuffix extends BaseFolder {
     public void fileOper(File file) {
         this.deleteSuffix(file);
     }
 }
 //如果文件增加后缀
 class AddFileSuffix extends BaseFolder {
     public void fileOper(File file) {
         this.addSuffix(file);
     }
 }

 //删除文件夹名和指定名字一样的文件夹,不考虑大小写.
 class DeleteFolderByName extends BaseFolder {
     private String pattern = null;
     public DeleteFolderByName(String pattern)
     {
         this.pattern = pattern;
     }
     public void folderOper(File file)
     {
         deleteFolder(file);
     }
     public boolean getCondition(File file)
     {
         return file.getName().equalsIgnoreCase(pattern);
     }
 }
 //如果文件名包含指定pattern,就将文件删除
 class DeleteFileByPattern extends BaseFolder {
     private String pattern = null;
     public DeleteFileByPattern(String pattern)
     {
         this.pattern = pattern;
     }   
     public void fileOper(File file) {
         if(pattern==null)
             return;
         String fileName = file.getName();
         if(fileName.contains(pattern))
             file.delete ();
     }
 }
 //删除具有指定后缀名的文件
 class DeleteFileBySuffix extends BaseFolder {
     private String suffix;
     public DeleteFileBySuffix(String suffix)
     {
         this.suffix = suffix;
     }
     public void fileOper(File file) {
         String path = file.getAbsolutePath();
         if(path.endsWith(suffix))
             file.delete();
     }
 }
 //以依次增加的数字编号重命名文件
 class RenameFileLikeNumber extends BaseFolder
 {
     private final String WILDCARD = "#";  //通配符
     private String pattern = null;
     private long startNumber = 0;
     private long count = 0;
     
     public RenameFileLikeNumber(){}
     public RenameFileLikeNumber(long startNumber)
     {
         this.startNumber = startNumber;
         count = startNumber;
     }
     public RenameFileLikeNumber(long startNumber,String pattern)
     {
         this.startNumber = startNumber;
         this.pattern = pattern;
         count = startNumber;
     }
     public void fileOper(File file) {
         this.rename(file,getNameForFile());
         count++;
     }
     private String getNameForFile()
     {
         String fileName = "";
         if(pattern!=null)
         {
          int i = pattern.length();
          int j =  Long.toString(count).length();
          for(int k=j;k<i;k++)
              fileName += "0";
          fileName += count+"";
          return fileName;
         }
         else
             return count+"";
     }
 }

星期日, 十月 14, 2007

关闭开机Ctrl + Alt + Del组合键

Start→Administrative Tools→Local security policy→Local Policies→Security Options→Interactive Logon:Do not require CTRL+ALT+DEL

关闭win2003事件跟踪程序

Run --->gpedit.msc--->Administrative Templates--->System--->Display Shutdown Event tracker

星期三, 十月 10, 2007

CSV文件格式

CSV即Comma Separate Values,这种文件格式经常用来作为不同程序之间的数据交互的格式。

具体文件格式

  • 每条记录占一行
  • 以逗号为分隔符
  • 逗号前后的空格会被忽略
  • 字段中包含有逗号,该字段必须用双引号括起来
  • 字段中包含有换行符,该字段必须用双引号括起来
  • 字段前后包含有空格,该字段必须用双引号括起来
  • 字段中的双引号用两个双引号表示
  • 字段中如果有双引号,该字段必须用双引号括起来
  • 第一条记录,可以是字段名
  • The CSV File Format


     

    • Each record is one line   ...but
      A record separator may consist of a line feed (ASCII/LF=0x0A), or a carriage return and line feed pair (ASCII/CRLF=0x0D 0x0A).
      ...but: fields may contain embedded line-breaks ( see below) so a record may span more than one line.

    • Fields are separated with commas.
      Example John,Doe,120 any st.,"Anytown, WW",08123

    • Leading and trailing space-characters adjacent to comma field separators are ignored.
      So   John  ,   Doe  ,... resolves to "John" and "Doe", etc. Space characters can be spaces, or tabs.

    • Fields with embedded commas must be delimited with double-quote characters.
      In the above example. "Anytown, WW" had to be delimited in double quotes because it had an embedded comma.

    • Fields that contain double quote characters must be surounded by double-quotes, and the embedded double-quotes must each be represented by a pair of consecutive double quotes.
      So, John "Da Man" Doe would convert to "John ""Da Man""",Doe, 120 any st.,...

    • A field that contains embedded line-breaks must be surounded by double-quotes
      So:
        Field 1: Conference room 1  
        Field 2:
          John,
          Please bring the M. Mathers file for review  
          -J.L.
        Field 3: 10/18/2002
        ...

      would convert to:

        Conference room 1, "John,  
        Please bring the M. Mathers file for review  
        -J.L.
        ",10/18/2002,...

      Note that this is a single CSV record, even though it takes up more than one line in the CSV file. This works because the line breaks are embedded inside the double quotes of the field.

      Implementation note: In Excel, leading spaces between the comma used for a field sepparator and the double quote will sometimes cause fields to be read in as unquoted fields, even though the first non-space character is a double quote. To avoid this quirk, simply remove all leading spaces after the field-sepparator comma and before the double quote character in your CSV export files.

    • Fields with leading or trailing spaces must be delimited with double-quote characters.
      So to preserve the leading and trailing spaces around the last name above: John ,"   Doe   ",...

      Usage note: Some applications will insist on helping you by removing leading and trailing spaces from all fields regardless of whether the CSV used quotes to preserve them. They may also insist on removing leading zeros from all fields regardless of whether you need them. One such application is Excel. :-( For some help with this quirk, see the section below entitled Excel vs. Leading Zero & Space .

    • Fields may always be delimited with double quotes.
      The delimiters will always be discarded.

      Implementation note: When importing CSV, do not reach down a layer and try to use the quotes to impart type information to fields. Also, when exporting CSV, you may want to be defensive of apps that improperly try to do this. Though, to be honest, I have not found any examples of applications that try to do this. If you have encountered any apps that attempt to use the quotes to glean type information from CSV files (like assuming quoted fields are strings even if they are numeric), please let me know about it.

    • The first record in a CSV file may be a header record containing column (field) names
      There is no mechanism for automatically discerning if the first record is a header row, so in the general case, this will have to be provided by an outside process (such as prompting the user). The header row is encoded just like any other CSV record in accordance with the rules above. A header row for the multi-line example above, might be:
        Location, Notes, "Start Date", ...

     

    星期一, 十月 01, 2007

    安装NOTES db的ODBC驱动,errorcode 126问题

    安装NotesSQL3.02j(文件名一般为C959CEN.exe);
    运行NotesSQL Authentication List Manager,设置notes.ini跟ID文件
    进入控制面板,管理工具,ODBC DataSource Administrator。新建DSN,这时候会有125错误,大体意思指*.nsf无法读取。
    不知道中文下提示信息具体是什么。
     
    解决方法:将notes.ini所在目录(通常为:C:\Program Files\Lotus\Notes)跟dll文件所在目录(通常为:C:\NotesSQL)加到环境变量Path中。
    问题解决。