首页

关于fedora源码包中FileUtils文件工具类对文件复制、删除及属性文件加载loadMap等处理

标签:FileUtils,文件工具类,fedora     发布时间:2018-04-26   

一、前言

关于fedora源码包中的fedora.utilities.FileUtils文件工具类,对文件流Stream、文件File复制copy、指定文件路径及文件删除delete、移动move、指定属性文件加载转换Map处理loadMap等

二、源码说明

package fedora.utilities;@b@@b@import java.io.File;@b@import java.io.FileFilter;@b@import java.io.FileInputStream;@b@import java.io.FileOutputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.OutputStream;@b@import java.util.HashMap;@b@import java.util.Map;@b@import java.util.Map.Entry;@b@import java.util.Properties;@b@import java.util.Set;@b@@b@public class FileUtils@b@{@b@  private static final int BUFF_SIZE = 100000;@b@  private static final byte[] buffer = new byte[100000];@b@@b@  public static boolean copy(InputStream source, OutputStream destination)@b@  {@b@    try@b@    {@b@      while (true)@b@        synchronized (buffer) {@b@          int amountRead = source.read(buffer);@b@          if (amountRead != -1) break label24;@b@          break label48:@b@@b@          label24: destination.write(buffer, 0, amountRead);@b@        }@b@@b@      label48: destination.flush();@b@      destination.close();@b@      return true; } catch (IOException e) {@b@    }@b@    return false;@b@  }@b@@b@  public static boolean copy(File source, File destination)@b@  {@b@    boolean result = true;@b@    if (source.isDirectory()) {@b@      if (destination.exists())@b@        result = (result) && (destination.isDirectory());@b@      else@b@        result = (result) && (destination.mkdirs());@b@@b@      File[] children = source.listFiles();@b@      File[] arr$ = children; int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { File element = arr$[i$];@b@        result = (result) && (copy(new File(source, element.getName()), new File(destination, element.getName())));@b@      }@b@@b@      return result;@b@    }@b@    try {@b@      InputStream in = new FileInputStream(source);@b@      OutputStream out = new FileOutputStream(destination);@b@      result = (result) && (copy(in, out));@b@      in.close();@b@      out.close();@b@      return result; } catch (IOException e) {@b@    }@b@    return false;@b@  }@b@@b@  public static File createTempDir(String prefix, File directory)@b@    throws IOException@b@  {@b@    File tempFile = File.createTempFile(prefix, "", directory);@b@    if (!(tempFile.delete()))@b@      throw new IOException();@b@    if (!(tempFile.mkdir()))@b@      throw new IOException();@b@    return tempFile;@b@  }@b@@b@  public static boolean delete(File file)@b@  {@b@    boolean result = true;@b@@b@    if (file == null)@b@      return false;@b@@b@    if (file.exists()) {@b@      File[] arr$;@b@      int i$;@b@      if (file.isDirectory())@b@      {@b@        File[] children = file.listFiles();@b@        arr$ = children; int len$ = arr$.length; for (i$ = 0; i$ < len$; ++i$) { File child = arr$[i$];@b@          result = (result) && (delete(child));@b@        }@b@      }@b@      result = (result) && (file.delete());@b@    }@b@    return result;@b@  }@b@@b@  public static boolean delete(String file)@b@  {@b@    return delete(new File(file));@b@  }@b@@b@  public static boolean move(File source, File destination)@b@  {@b@    if ((source == null) || (destination == null))@b@      return false;@b@@b@    if (source.renameTo(destination))@b@      return true;@b@@b@    return ((copy(source, destination)) && (delete(source)));@b@  }@b@@b@  public static Properties loadProperties(File f)@b@    throws IOException@b@  {@b@    Properties props = new Properties();@b@    FileInputStream in = new FileInputStream(f);@b@    try {@b@      props.load(in);@b@      return props;@b@    } finally {@b@      try {@b@        in.close();@b@      }@b@      catch (IOException e)@b@      {@b@      }@b@    }@b@  }@b@@b@  public static Map<String, String> loadMap(File f)@b@    throws IOException@b@  {@b@    Properties props = new Properties();@b@    FileInputStream in = new FileInputStream(f);@b@    try {@b@      props.load(in);@b@      Map map = new HashMap();@b@      Set entrySet = props.entrySet();@b@      for (Map.Entry entry : entrySet)@b@      {@b@        map.put((String)entry.getKey(), (String)entry.getValue());@b@      }@b@      return map;@b@    } finally {@b@      try {@b@        in.close();@b@      } catch (IOException e) {@b@      }@b@    }@b@  }@b@@b@  public static FileFilter getPrefixFileFilter(String prefix) {@b@    return new PrefixFileFilter(prefix);@b@  }@b@@b@  public static FileFilter getSuffixFileFilter(String suffix) {@b@    return new SuffixFileFilter(suffix);@b@  }@b@@b@  private static class SuffixFileFilter@b@    implements FileFilter@b@  {@b@    private final String filenameSuffix;@b@@b@    SuffixFileFilter(String filenameSuffix)@b@    {@b@      this.filenameSuffix = filenameSuffix;@b@    }@b@@b@    public boolean accept(File file) {@b@      String filename = file.getName();@b@      return filename.endsWith(this.filenameSuffix);@b@    }@b@  }@b@@b@  private static class PrefixFileFilter@b@    implements FileFilter@b@  {@b@    private final String filenamePrefix;@b@@b@    PrefixFileFilter(String filenamePrefix)@b@    {@b@      this.filenamePrefix = filenamePrefix;@b@    }@b@@b@    public boolean accept(File file) {@b@      String filename = file.getName();@b@      return filename.startsWith(this.filenamePrefix);@b@    }@b@  }@b@}