小练习一则

JBoss Seam 1.2.1GA的发行包的src里,有一些文件中有莫名其妙的0xA0字符,本来想用shell搞定的,结果搞了半天也没有用sed弄出完整的解决方案,最后只好用Java语言写了,顺带着也练练groovy和ruby.

最初的Java代码是这样的:
java 代码
 
  1. import java.io.*;  
  2.   
  3. public class Wash {  
  4.     public static void main(String[] args) {  
  5.         File root = new File("c:/ProgramFiles/jboss-seam-1.2.1.GA/src");  
  6.         wash(new File("c:/temp/seamsrc"));  
  7.     }  
  8.   
  9.     private static void washFile(File file) throws IOException {  
  10.         String path = file.getAbsolutePath();  
  11.         System.out.println("Washing file: " + path);  
  12.         File temp = new File(path + ".bak");  
  13.         temp.createNewFile();  
  14.         FileInputStream in = new FileInputStream(file);  
  15.         FileOutputStream out = new FileOutputStream(temp);  
  16.         byte bbyte;  
  17.         boolean modified = false;  
  18.         while ((bbyte = (byte) in.read()) != -1) {  
  19.             if (bbyte != (byte0xa0) {  
  20.                 out.write(bbyte);  
  21.             } else {  
  22.                 modified = true;  
  23.             }  
  24.         }  
  25.         in.close();  
  26.         out.close();  
  27.         if (modified) {  
  28.             file.delete();  
  29.             temp.renameTo(new File(path));  
  30.         } else {  
  31.             temp.delete();  
  32.         }  
  33.     }  
  34.   
  35.     private static void washDir(File dir) throws IOException {  
  36.         for (File file : dir.listFiles()) {  
  37.             if (file.isDirectory()) {  
  38.                 washDir(file);  
  39.             } else {  
  40.                 washFile(file);  
  41.             }  
  42.         }  
  43.     }  
  44. }  
想了想washFile和washDir可以合并,于是refactor了一下,成为这样
java 代码
 
  1. import java.io.*;  
  2.   
  3. public class Wash {  
  4.     public static void main(String[] args) {  
  5.         wash(new File("c:/ProgramFiles/jboss-seam-1.2.1.GA/src"));  
  6.     }  
  7.   
  8.     private static void wash(File file) {  
  9.         if (file.isFile()) {  
  10.             String path = file.getAbsolutePath();  
  11.             System.out.print("Washing file: " + path+" ...");  
  12.             File temp = new File(path + ".bak");  
  13.             FileInputStream in = null;  
  14.             FileOutputStream out = null;  
  15.             boolean modified = false;  
  16.   
  17.             try {  
  18.                 temp.createNewFile();  
  19.                 in = new FileInputStream(file);  
  20.                 out = new FileOutputStream(temp);  
  21.                 byte bbyte;  
  22.                 while ((bbyte = (byte) in.read()) != -1) {  
  23.                     if (bbyte != (byte0xa0) {  
  24.                         out.write(bbyte);  
  25.                     } else {  
  26.                         modified = true;  
  27.                     }  
  28.                 }  
  29.             } catch (IOException e) {  
  30.                 e.printStackTrace();  
  31.                 System.exit(-1);  
  32.             } finally {  
  33.                 try {  
  34.                     assert (in != null && out != null);  
  35.                     in.close();  
  36.                     out.close();  
  37.                 } catch (IOException e) {  
  38.                     e.printStackTrace();  
  39.                 }  
  40.             }  
  41.             if (modified) {  
  42.                 file.delete();  
  43.                 temp.renameTo(new File(path));  
  44.                 System.out.println("File "+file.getAbsolutePath()+" washed.");  
  45.             } else {  
  46.                 System.out.println("Not modified, removing temp file "+temp.getAbsolutePath());  
  47.                 temp.delete();  
  48.             }  
  49.         } else {  
  50.             for (File child : file.listFiles()) {  
  51.                 wash(child);  
  52.             }  
  53.         }  
  54.     }  
  55.   
  56. }  

下面是groovy的版本 :
java 代码
 
  1. wash = { dir ->  
  2.     dir.eachFile { child ->  
  3.         if(child.isFile()){  
  4.             path = child.absolutePath  
  5.             print "washing file "+path+"..."  
  6.             temp = new File(path+".bak")  
  7.             modified = false;  
  8.             temp.withOutputStream { outputStream ->  
  9.                 child.eachByte{ bbyte ->  
  10.                     if(bbyte != (byte)0xa0){  
  11.                         outputStream.write(bbyte)  
  12.                     } else {  
  13.                       modified = true  
  14.                     }  
  15.                 }  
  16.             }  
  17.            if(modified){  
  18.                child.delete()  
  19.                temp.renameTo(new File(path))  
  20.                println("File "+path +" washed.")  
  21.            } else {  
  22.                println("Not modified, removing temp file "+temp.absolutePath)  
  23.                temp.delete()  
  24.            }  
  25.         } else {  
  26.             println("washing subdirectory..."+child.absolutePath)  
  27.             wash(child)  
  28.         }  
  29.     }  
  30. }  
  31.   
  32. wash(new File("c:/temp/seamsrc"))  
groovy跟java还是亲啊,有了java版本,groovy版本很快就出来了。代码量确实有缩减,还了解到groovy的closure的作用域与def的使用有些关联。其实在这段代码中外层的wash closure跟method的定义已经没啥区别了。

在做ruby时,发现ruby的文件操作API极度混乱,在javaeye ruby版本发了点牢骚,还跟人 论战了一把,后来我的想法得到了松底迪的支持,挺高兴,于是就有了 fileex的实现。使用fileex后的ruby实现代码如下:
ruby 代码
 
  1. require "fileex"  
  2.   
  3. def wash(dir)  
  4.     dir.each|child|  
  5.         if child.directory?  
  6.             wash(child)  
  7.         else  
  8.             path = child.path  
  9.             puts "washing file #{path}..."  
  10.             temp = File.new("#{path}.bak").create.open("w+")  
  11.             modified = false  
  12.             child.open.each_byte{ |byte|  
  13.                 temp.putc(byte)  
  14.                 if byte!=0xA0  
  15.                     temp.putc(byte)  
  16.                 else  
  17.                     modified = true  
  18.                 end  
  19.             }  
  20.             child.close  
  21.             temp.close  
  22.             if modified  
  23.                 puts "File #{path} washed."  
  24.                 child.delete  
  25.                 temp.rename(path)  
  26.             else  
  27.                 temp.delete  
  28.             end  
  29.         end  
  30.     }  
  31. end  
  32.   
  33. wash("c:/temp/seamsrc".to_file)  
这下终于爽了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值