示例#1
0
 /**
  * @param file
  * @param encoding
  * @return
  */
 public static String read(File file, String encoding) {
   try {
     return new String(read(file), encoding);
   } catch (UnsupportedEncodingException e) {
     throw Lang.uncheck(e);
   }
 }
示例#2
0
 /**
  * @param file
  * @return
  */
 public static String canonical(File file) {
   try {
     return file.getCanonicalPath();
   } catch (Exception e) {
     throw Lang.uncheck(e);
   }
 }
示例#3
0
  /**
   * @param source
   * @param target
   */
  public static void copy(File source, File target) {
    try {

      new CopyFileCommand(source, target).execute();
    } catch (Exception e) {
      throw Lang.uncheck(e);
    }
  }
示例#4
0
  /**
   * @param file
   * @param data
   * @param encoding
   */
  public static void appendTo(File file, String data, String encoding) {
    try {

      appendTo(file, data.getBytes(encoding));
    } catch (UnsupportedEncodingException e) {
      throw Lang.uncheck(e);
    }
  }
示例#5
0
  /**
   * @param file
   * @param content
   * @param encoding
   */
  public static void write(File file, String content, String encoding) {

    try {

      write(file, content.getBytes(encoding));
    } catch (Exception e) {
      Lang.uncheck(e);
    }
  }
示例#6
0
  /**
   * @param file
   * @param content
   */
  public static void write(File file, byte[] content) {

    try {

      new MakeFileCommand(file).execute();
      if (file.isDirectory()) {
        throw new IllegalStateException("file[" + file.getName() + "] is a directory!");
      }

      new WriteBytesToFileCommand(file, content).execute();
    } catch (Exception e) {
      throw Lang.uncheck(e);
    }
  }
示例#7
0
  /**
   * @param file
   * @return
   */
  public static byte[] read(File file) {

    if (!file.exists() || !file.canRead()) {
      throw new IllegalStateException("file is not exist or can not read!");
    }

    ByteArrayOutputStream out = null;
    try {

      out = new ByteArrayOutputStream();
      new WriteFileToCommand(file, out, true).execute();
      return out.toByteArray();
    } catch (Exception e) {
      throw Lang.uncheck(e);
    } finally {
      IOs.freeQuietly(out);
    }
  }
示例#8
0
  /**
   * @param oldname
   * @param newname
   */
  public static void rename(String oldname, String newname) {
    try {

      File file = new File(oldname);
      if (!file.exists()) {
        throw new FileNotFoundException("the old file is not exist!");
      }

      File newFile = new File(newname);
      if (newFile.exists()) {
        throw new IllegalStateException("the new file is exist!");
      }

      file.renameTo(newFile);
    } catch (Exception e) {
      throw Lang.uncheck(e);
    }
  }