java 文件处理

Java代码
  1. package javax.org.path;   
  2. import java.math.BigDecimal;   
  3. /**  
  4.  * @Author:jilongliang  
  5.  * @Date :2013-6-18  
  6.  * @Project:JTool  
  7.  * @Class:AccessFile.java  
  8.  * @Description:文件处理类  
  9.  */  
  10. public class AccessFile {   
  11.     public static final long KB = 1024;//KB   
  12.     public static final long MB = KB * KB;//MB   
  13.     public static final long GB = KB * MB;//GB   
  14.     /**  
  15.      * 处理文件大小  
  16.      */  
  17.     public static String fileSize(long file) {   
  18.         if (file <= 0) {   
  19.             return "";   
  20.         } else if (file < MB) {   
  21.             BigDecimal b = new BigDecimal((double) file / KB);   
  22.             return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "K";   
  23.         } else if (file < GB) {   
  24.             BigDecimal b = new BigDecimal((double) file / MB);   
  25.             return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "M";   
  26.         } else {   
  27.             BigDecimal b = new BigDecimal((double) file / GB);   
  28.             return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "G";   
  29.         }   
  30.     }   
  31.     /**获取当前工程路径  
  32.      * @return  
  33.      */  
  34.     public static String getSysPath() {   
  35.         //String path = Thread.currentThread().getContextClassLoader().getResource("").toString();   
  36.         String path = Thread.currentThread().getContextClassLoader().getResource(".").toString();   
  37.         String temp = path.replaceFirst("file:/""").replaceFirst("WEB-INF/classes/""");   
  38.         String separator = System.getProperty("file.separator");   
  39.         String resultPath = temp.replaceAll("/", separator + separator);   
  40.         return resultPath;   
  41.     }   
  42.     /**   
  43.      * Thread.currentThread().getContextClassLoader().getResource("")  
  44.      * 的getResource里面空串或者点或者是/输出的路径是一致  
  45.      * "" D:Eclipse3.7JTooluildclasses  
  46.      * .  D:Eclipse3.7JTooluildclasses  
  47.      * -/ D:Eclipse3.7JTooluildclasses  
  48.      * @return  
  49.      */  
  50.     public static String getClassPath() {   
  51.         //String path = Thread.currentThread().getContextClassLoader().getResource("").toString();   
  52.         //String path = Thread.currentThread().getContextClassLoader().getResource(".").toString();   
  53.         String path = Thread.currentThread().getContextClassLoader().getResource("/").toString();   
  54.         String temp = path.replaceFirst("file:/""");   
  55.         String separator = System.getProperty("file.separator");   
  56.         String resultPath = temp.replaceAll("/", separator + separator);   
  57.         return resultPath;   
  58.     }   
  59.     /**  
  60.      * getClassLoader().getResource()方法参数空串和点都是输出相同的路径唯有/是报空指针  
  61.      * "" D:Eclipse3.7JTooluildclasses  
  62.      * .  D:Eclipse3.7JTooluildclasses  
  63.      *-/  报空指针  
  64.      * @return  
  65.      */  
  66.     private  String getClassesAbsolutePath(){   
  67.         // 得到的是 项目的绝对路径   
  68.         String path=this.getClass().getClassLoader().getResource("").getPath();   
  69.         //String path=this.getClass().getClassLoader().getResource(".").getPath();   
  70.         //String path=this.getClass().getClassLoader().getResource("/").getPath();//报空指针   
  71.         String temp = path.replaceFirst("/""");   
  72.         String separator = System.getProperty("file.separator");   
  73.         String resultPath = temp.replaceAll("/", separator + separator);   
  74.         return resultPath;   
  75.     }   
  76.     /**  
  77.      *得到的是当前类 文件的URI目录,不包括自己  
  78.      * ""D:Eclipse3.7JTooluildclassesjavaxorgpath  
  79.      * . D:Eclipse3.7JTooluildclassesjavaxorgpath  
  80.      - / D:Eclipse3.7JTooluildclasses  
  81.      * @return  
  82.      */  
  83.     private String getCurrentClassPath(){   
  84.         //String path=this.getClass().getResource("").getPath();   
  85.         //String path=this.getClass().getResource(".").getPath();   
  86.         String path=this.getClass().getResource("/").getPath();   
  87.         String temp = path.replaceFirst("/""");   
  88.         String separator = System.getProperty("file.separator");   
  89.         String resultPath = temp.replaceAll("/", separator + separator);   
  90.         return resultPath;   
  91.     }   
  92.     public static void main(String[] args) {   
  93.         System.out.println(getSysPath());   
  94.     }   
  95. }  
原文地址:https://www.cnblogs.com/win7xt/p/3143882.html