zoukankan      html  css  js  c++  java
  • 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. }  
  • 相关阅读:
    .NetCore Grpc 客服端 工厂模式配置授权
    DOCKER 拉取 dotnet 镜像太慢 docker pull mcr.microsoft.com too slow
    Introducing .NET 5
    VSCode 出现错误 System.IO.IOException: The configured user limit (128) on the number of inotify instances has been reached.
    Omnisharp VsCode Attaching to remote processes
    zookeeper3.5.5 centos7 完全分布式 搭建随记
    Hadoop2.7.7 centos7 完全分布式 配置与问题随记
    MySQL索引 索引分类 最左前缀原则 覆盖索引 索引下推 联合索引顺序
    SQL基础随记3 范式 键
    MySQL调优 优化需要考虑哪些方面
  • 原文地址:https://www.cnblogs.com/win7xt/p/3143882.html
Copyright © 2011-2022 走看看