最初的Java代码是这样的:
java 代码
- import java.io.*;
- public class Wash {
- public static void main(String[] args) {
- File root = new File("c:/ProgramFiles/jboss-seam-1.2.1.GA/src");
- wash(new File("c:/temp/seamsrc"));
- }
- private static void washFile(File file) throws IOException {
- String path = file.getAbsolutePath();
- System.out.println("Washing file: " + path);
- File temp = new File(path + ".bak");
- temp.createNewFile();
- FileInputStream in = new FileInputStream(file);
- FileOutputStream out = new FileOutputStream(temp);
- byte bbyte;
- boolean modified = false;
- while ((bbyte = (byte) in.read()) != -1) {
- if (bbyte != (byte) 0xa0) {
- out.write(bbyte);
- } else {
- modified = true;
- }
- }
- in.close();
- out.close();
- if (modified) {
- file.delete();
- temp.renameTo(new File(path));
- } else {
- temp.delete();
- }
- }
- private static void washDir(File dir) throws IOException {
- for (File file : dir.listFiles()) {
- if (file.isDirectory()) {
- washDir(file);
- } else {
- washFile(file);
- }
- }
- }
- }
java 代码
- import java.io.*;
- public class Wash {
- public static void main(String[] args) {
- wash(new File("c:/ProgramFiles/jboss-seam-1.2.1.GA/src"));
- }
- private static void wash(File file) {
- if (file.isFile()) {
- String path = file.getAbsolutePath();
- System.out.print("Washing file: " + path+" ...");
- File temp = new File(path + ".bak");
- FileInputStream in = null;
- FileOutputStream out = null;
- boolean modified = false;
- try {
- temp.createNewFile();
- in = new FileInputStream(file);
- out = new FileOutputStream(temp);
- byte bbyte;
- while ((bbyte = (byte) in.read()) != -1) {
- if (bbyte != (byte) 0xa0) {
- out.write(bbyte);
- } else {
- modified = true;
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- System.exit(-1);
- } finally {
- try {
- assert (in != null && out != null);
- in.close();
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (modified) {
- file.delete();
- temp.renameTo(new File(path));
- System.out.println("File "+file.getAbsolutePath()+" washed.");
- } else {
- System.out.println("Not modified, removing temp file "+temp.getAbsolutePath());
- temp.delete();
- }
- } else {
- for (File child : file.listFiles()) {
- wash(child);
- }
- }
- }
- }
下面是groovy的版本 :
java 代码
- wash = { dir ->
- dir.eachFile { child ->
- if(child.isFile()){
- path = child.absolutePath
- print "washing file "+path+"..."
- temp = new File(path+".bak")
- modified = false;
- temp.withOutputStream { outputStream ->
- child.eachByte{ bbyte ->
- if(bbyte != (byte)0xa0){
- outputStream.write(bbyte)
- } else {
- modified = true
- }
- }
- }
- if(modified){
- child.delete()
- temp.renameTo(new File(path))
- println("File "+path +" washed.")
- } else {
- println("Not modified, removing temp file "+temp.absolutePath)
- temp.delete()
- }
- } else {
- println("washing subdirectory..."+child.absolutePath)
- wash(child)
- }
- }
- }
- wash(new File("c:/temp/seamsrc"))
在做ruby时,发现ruby的文件操作API极度混乱,在javaeye ruby版本发了点牢骚,还跟人 论战了一把,后来我的想法得到了松底迪的支持,挺高兴,于是就有了 fileex的实现。使用fileex后的ruby实现代码如下:
ruby 代码
- require "fileex"
- def wash(dir)
- dir.each{ |child|
- if child.directory?
- wash(child)
- else
- path = child.path
- puts "washing file #{path}..."
- temp = File.new("#{path}.bak").create.open("w+")
- modified = false
- child.open.each_byte{ |byte|
- temp.putc(byte)
- if byte!=0xA0
- temp.putc(byte)
- else
- modified = true
- end
- }
- child.close
- temp.close
- if modified
- puts "File #{path} washed."
- child.delete
- temp.rename(path)
- else
- temp.delete
- end
- end
- }
- end
- wash("c:/temp/seamsrc".to_file)