栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java文件操作工具类fileUtil实例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java文件操作工具类fileUtil实例

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

package com.gcloud.common;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

public class FileUtil {
  
  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();
  }
  
  public static boolean isFileExist(String fileName) {
    return new File(fileName).isFile();
  }
  
  public static boolean makeDirectory(File file) {
    File parent = file.getParentFile();
    if (parent != null) {
      return parent.mkdirs();
    }
    return false;
  }
  
  public static URL getURL(File file) throws MalformedURLException {
    String fileURL = "file:/" + file.getAbsolutePath();
    URL url = new URL(fileURL);
    return url;
  }
  
  public static String getFileName(String filePath) {
    File file = new File(filePath);
    return file.getName();
  }
  
  public static String getFilePath(String fileName) {
    File file = new File(fileName);
    return file.getAbsolutePath();
  }
  
  public static String toUNIXpath(String filePath) {
    return filePath.replace("", "/");
  }
  
  public static String getUNIXfilePath(String fileName) {
    File file = new File(fileName);
    return toUNIXpath(file.getAbsolutePath());
  }
  
  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);
    }
  }
  
  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);
    }
  }
  
  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);
    }
  }
  
  public static int getPathLastIndex(String fileName) {
    int point = fileName.lastIndexOf("/");
    if (point == -1) {
      point = fileName.lastIndexOf("");
    }
    return point;
  }
  
  public static int getPathLastIndex(String fileName, int fromIndex) {
    int point = fileName.lastIndexOf("/", fromIndex);
    if (point == -1) {
      point = fileName.lastIndexOf("", fromIndex);
    }
    return point;
  }
  
  public static int getPathIndex(String fileName) {
    int point = fileName.indexOf("/");
    if (point == -1) {
      point = fileName.indexOf("");
    }
    return point;
  }
  
  public static int getPathIndex(String fileName, int fromIndex) {
    int point = fileName.indexOf("/", fromIndex);
    if (point == -1) {
      point = fileName.indexOf("", fromIndex);
    }
    return point;
  }
  
  public static String removeFileExt(String filename) {
    int index = filename.lastIndexOf(".");
    if (index != -1) {
      return filename.substring(0, index);
    } else {
      return filename;
    }
  }
  
  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;
    }
  }
  
  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);
    }
  }
  
  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();
  }
  
  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;
      }
    }
  }
  
  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程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/143521.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号