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. }  
  • 相关阅读:
    Kali Linux下安装配置ProFTPD实例
    mysql 如何用root 登录
    串口总是报'Error opening serial port'
    用SPCOMM 在 Delphi中实现串口通讯 转
    delphi中使用spcomm来实现串口通讯(转载)
    SPCOMM的一些用法注意
    MySQL 字符串 转 int/double CAST与CONVERT 函数的用法
    彻底删除mysql服务
    mysql 非安装版的一个自动安装脚本及工具(更新版)
    bat操作数据库mysql
  • 原文地址:https://www.cnblogs.com/win7xt/p/3143882.html
Copyright © 2011-2022 走看看