- package javax.org.path;
- import java.math.BigDecimal;
- /**
- * @Author:jilongliang
- * @Date :2013-6-18
- * @Project:JTool
- * @Class:AccessFile.java
- * @Description:文件处理类
- */
- public class AccessFile {
- public static final long KB = 1024;//KB
- public static final long MB = KB * KB;//MB
- public static final long GB = KB * MB;//GB
- /**
- * 处理文件大小
- */
- public static String fileSize(long file) {
- if (file <= 0) {
- return "";
- } else if (file < MB) {
- BigDecimal b = new BigDecimal((double) file / KB);
- return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "K";
- } else if (file < GB) {
- BigDecimal b = new BigDecimal((double) file / MB);
- return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "M";
- } else {
- BigDecimal b = new BigDecimal((double) file / GB);
- return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "G";
- }
- }
- /**获取当前工程路径
- * @return
- */
- public static String getSysPath() {
- //String path = Thread.currentThread().getContextClassLoader().getResource("").toString();
- String path = Thread.currentThread().getContextClassLoader().getResource(".").toString();
- String temp = path.replaceFirst("file:/", "").replaceFirst("WEB-INF/classes/", "");
- String separator = System.getProperty("file.separator");
- String resultPath = temp.replaceAll("/", separator + separator);
- return resultPath;
- }
- /**
- * Thread.currentThread().getContextClassLoader().getResource("")
- * 的getResource里面空串或者点或者是/输出的路径是一致
- * "" D:Eclipse3.7JTooluildclasses
- * . D:Eclipse3.7JTooluildclasses
- * -/ D:Eclipse3.7JTooluildclasses
- * @return
- */
- public static String getClassPath() {
- //String path = Thread.currentThread().getContextClassLoader().getResource("").toString();
- //String path = Thread.currentThread().getContextClassLoader().getResource(".").toString();
- String path = Thread.currentThread().getContextClassLoader().getResource("/").toString();
- String temp = path.replaceFirst("file:/", "");
- String separator = System.getProperty("file.separator");
- String resultPath = temp.replaceAll("/", separator + separator);
- return resultPath;
- }
- /**
- * getClassLoader().getResource()方法参数空串和点都是输出相同的路径唯有/是报空指针
- * "" D:Eclipse3.7JTooluildclasses
- * . D:Eclipse3.7JTooluildclasses
- *-/ 报空指针
- * @return
- */
- private String getClassesAbsolutePath(){
- // 得到的是 项目的绝对路径
- String path=this.getClass().getClassLoader().getResource("").getPath();
- //String path=this.getClass().getClassLoader().getResource(".").getPath();
- //String path=this.getClass().getClassLoader().getResource("/").getPath();//报空指针
- String temp = path.replaceFirst("/", "");
- String separator = System.getProperty("file.separator");
- String resultPath = temp.replaceAll("/", separator + separator);
- return resultPath;
- }
- /**
- *得到的是当前类 文件的URI目录,不包括自己
- * ""D:Eclipse3.7JTooluildclassesjavaxorgpath
- * . D:Eclipse3.7JTooluildclassesjavaxorgpath
- - / D:Eclipse3.7JTooluildclasses
- * @return
- */
- private String getCurrentClassPath(){
- //String path=this.getClass().getResource("").getPath();
- //String path=this.getClass().getResource(".").getPath();
- String path=this.getClass().getResource("/").getPath();
- String temp = path.replaceFirst("/", "");
- String separator = System.getProperty("file.separator");
- String resultPath = temp.replaceAll("/", separator + separator);
- return resultPath;
- }
- public static void main(String[] args) {
- System.out.println(getSysPath());
- }
- }