当前位置: 移动技术网 > IT编程>开发语言>Java > Java文件操作工具类fileUtil实例【文件增删改,复制等】

Java文件操作工具类fileUtil实例【文件增删改,复制等】

2019年07月19日  | 移动技术网IT编程  | 我要评论

本文实例讲述了java文件操作工具类fileutil。分享给大家供大家参考,具体如下:

package com.gcloud.common;
import java.io.*;
import java.net.malformedurlexception;
import java.net.url;
/**
 * 文件工具类
 * created by charlin on 2017/9/8.
 */
public class fileutil {
  /**
   * 读取文件内容
   *
   * @param is
   * @return
   */
  public static string readfile(inputstream is) {
    bufferedreader br = null;
    stringbuffer sb = new stringbuffer();
    try {
      br = new bufferedreader(new inputstreamreader(is, "utf-8"));
      string readline = null;
      while ((readline = br.readline()) != null) {
        sb.append(readline);
      }
    } catch (exception e) {
      e.printstacktrace();
    } finally {
      try {
        br.close();
        is.close();
      } catch (ioexception e) {
        e.printstacktrace();
      }
    }
    return sb.tostring();
  }
  /**
   * 判断指定的文件是否存在。
   *
   * @param filename
   * @return
   */
  public static boolean isfileexist(string filename) {
    return new file(filename).isfile();
  }
  /**
   * 创建指定的目录。 如果指定的目录的父目录不存在则创建其目录书上所有需要的父目录。
   * 注意:可能会在返回false的时候创建部分父目录。
   *
   * @param file
   * @return
   */
  public static boolean makedirectory(file file) {
    file parent = file.getparentfile();
    if (parent != null) {
      return parent.mkdirs();
    }
    return false;
  }
  /**
   * 返回文件的url地址。
   *
   * @param file
   * @return
   * @throws malformedurlexception
   */
  public static url geturl(file file) throws malformedurlexception {
    string fileurl = "file:/" + file.getabsolutepath();
    url url = new url(fileurl);
    return url;
  }
  /**
   * 从文件路径得到文件名。
   *
   * @param filepath
   * @return
   */
  public static string getfilename(string filepath) {
    file file = new file(filepath);
    return file.getname();
  }
  /**
   * 从文件名得到文件绝对路径。
   *
   * @param filename
   * @return
   */
  public static string getfilepath(string filename) {
    file file = new file(filename);
    return file.getabsolutepath();
  }
  /**
   * 将dos/windows格式的路径转换为unix/linux格式的路径。
   *
   * @param filepath
   * @return
   */
  public static string tounixpath(string filepath) {
    return filepath.replace("", "/");
  }
  /**
   * 从文件名得到unix风格的文件绝对路径。
   *
   * @param filename
   * @return
   */
  public static string getunixfilepath(string filename) {
    file file = new file(filename);
    return tounixpath(file.getabsolutepath());
  }
  /**
   * 得到文件后缀名
   *
   * @param filename
   * @return
   */
  public static string getfileext(string filename) {
    int point = filename.lastindexof('.');
    int length = filename.length();
    if (point == -1 || point == length - 1) {
      return "";
    } else {
      return filename.substring(point + 1, length);
    }
  }
  /**
   * 得到文件的名字部分。 实际上就是路径中的最后一个路径分隔符后的部分。
   *
   * @param filename
   * @return
   */
  public static string getnamepart(string filename) {
    int point = getpathlastindex(filename);
    int length = filename.length();
    if (point == -1) {
      return filename;
    } else if (point == length - 1) {
      int secondpoint = getpathlastindex(filename, point - 1);
      if (secondpoint == -1) {
        if (length == 1) {
          return filename;
        } else {
          return filename.substring(0, point);
        }
      } else {
        return filename.substring(secondpoint + 1, point);
      }
    } else {
      return filename.substring(point + 1);
    }
  }
  /**
   * 得到文件名中的父路径部分。 对两种路径分隔符都有效。 不存在时返回""。
   * 如果文件名是以路径分隔符结尾的则不考虑该分隔符,例如"/path/"返回""。
   *
   * @param filename
   * @return
   */
  public static string getpathpart(string filename) {
    int point = getpathlastindex(filename);
    int length = filename.length();
    if (point == -1) {
      return "";
    } else if (point == length - 1) {
      int secondpoint = getpathlastindex(filename, point - 1);
      if (secondpoint == -1) {
        return "";
      } else {
        return filename.substring(0, secondpoint);
      }
    } else {
      return filename.substring(0, point);
    }
  }
  /**
   * 得到路径分隔符在文件路径中最后出现的位置。 对于dos或者unix风格的分隔符都可以。
   *
   * @param filename
   * @return
   */
  public static int getpathlastindex(string filename) {
    int point = filename.lastindexof("/");
    if (point == -1) {
      point = filename.lastindexof("");
    }
    return point;
  }
  /**
   * 得到路径分隔符在文件路径中指定位置前最后出现的位置。 对于dos或者unix风格的分隔符都可以。
   *
   * @param filename
   * @param fromindex
   * @return
   */
  public static int getpathlastindex(string filename, int fromindex) {
    int point = filename.lastindexof("/", fromindex);
    if (point == -1) {
      point = filename.lastindexof("", fromindex);
    }
    return point;
  }
  /**
   * 得到路径分隔符在文件路径中首次出现的位置。 对于dos或者unix风格的分隔符都可以。
   *
   * @param filename
   * @return
   */
  public static int getpathindex(string filename) {
    int point = filename.indexof("/");
    if (point == -1) {
      point = filename.indexof("");
    }
    return point;
  }
  /**
   * 得到路径分隔符在文件路径中指定位置后首次出现的位置。 对于dos或者unix风格的分隔符都可以。
   *
   * @param filename
   * @param fromindex
   * @return
   */
  public static int getpathindex(string filename, int fromindex) {
    int point = filename.indexof("/", fromindex);
    if (point == -1) {
      point = filename.indexof("", fromindex);
    }
    return point;
  }
  /**
   * 将文件名中的类型部分去掉。
   *
   * @param filename
   * @return
   */
  public static string removefileext(string filename) {
    int index = filename.lastindexof(".");
    if (index != -1) {
      return filename.substring(0, index);
    } else {
      return filename;
    }
  }
  /**
   * 得到相对路径。 文件名不是目录名的子节点时返回文件名。
   *
   * @param pathname
   * @param filename
   * @return
   */
  public static string getsubpath(string pathname, string filename) {
    int index = filename.indexof(pathname);
    if (index != -1) {
      return filename.substring(index + pathname.length() + 1);
    } else {
      return filename;
    }
  }
  /**
   * 删除一个文件。
   *
   * @param filename
   * @throws ioexception
   */
  public static void deletefile(string filename) throws ioexception {
    file file = new file(filename);
    if (file.isdirectory()) {
      throw new ioexception("ioexception -> badinputexception: not a file.");
    }
    if (!file.exists()) {
      throw new ioexception("ioexception -> badinputexception: file is not exist.");
    }
    if (!file.delete()) {
      throw new ioexception("cannot delete file. filename = " + filename);
    }
  }
  /**
   * 删除文件夹及其下面的子文件夹
   *
   * @param dir
   * @throws ioexception
   */
  public static void deletedir(file dir) throws ioexception {
    if (dir.isfile())
      throw new ioexception("ioexception -> badinputexception: not a directory.");
    file[] files = dir.listfiles();
    if (files != null) {
      for (int i = 0; i < files.length; i++) {
        file file = files[i];
        if (file.isfile()) {
          file.delete();
        } else {
          deletedir(file);
        }
      }
    }
    dir.delete();
  }
  /**
   * 复制文件
   *
   * @param src
   * @param dst
   * @throws exception
   */
  public static void copy(file src, file dst) throws exception {
    int buffer_size = 4096;
    inputstream in = null;
    outputstream out = null;
    try {
      in = new bufferedinputstream(new fileinputstream(src), buffer_size);
      out = new bufferedoutputstream(new fileoutputstream(dst), buffer_size);
      byte[] buffer = new byte[buffer_size];
      int len = 0;
      while ((len = in.read(buffer)) > 0) {
        out.write(buffer, 0, len);
      }
    } catch (exception e) {
      throw e;
    } finally {
      if (null != in) {
        try {
          in.close();
        } catch (ioexception e) {
          e.printstacktrace();
        }
        in = null;
      }
      if (null != out) {
        try {
          out.close();
        } catch (ioexception e) {
          e.printstacktrace();
        }
        out = null;
      }
    }
  }
  /**
   * @复制文件,支持把源文件内容追加到目标文件末尾
   * @param src
   * @param dst
   * @param append
   * @throws exception
   */
  public static void copy(file src, file dst, boolean append) throws exception {
    int buffer_size = 4096;
    inputstream in = null;
    outputstream out = null;
    try {
      in = new bufferedinputstream(new fileinputstream(src), buffer_size);
      out = new bufferedoutputstream(new fileoutputstream(dst, append), buffer_size);
      byte[] buffer = new byte[buffer_size];
      int len = 0;
      while ((len = in.read(buffer)) > 0) {
        out.write(buffer, 0, len);
      }
    } catch (exception e) {
      throw e;
    } finally {
      if (null != in) {
        try {
          in.close();
        } catch (ioexception e) {
          e.printstacktrace();
        }
        in = null;
      }
      if (null != out) {
        try {
          out.close();
        } catch (ioexception e) {
          e.printstacktrace();
        }
        out = null;
      }
    }
  }
}

更多关于java算法相关内容感兴趣的读者可查看本站专题:《java文件与目录操作技巧汇总》、《java数据结构与算法教程》、《java操作dom节点技巧总结》和《java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网