java.io.file修改名称_关于apache的commons-io源码包中FilenameUtils文件名称工具类对文件名称标准化、系统路径转换等操作源码说明..._偃鼠的博客-程序员宅基地

技术标签: java.io.file修改名称  

一、前言

关于apache的commons-io源码包org.apache.commons.io.FilenameUtils文件名称操作工具类,进行操作系统判断isSystemWindows、文件名称标准化normalize处理、不同操作系统unix路径转换separatorsToUnix、通过文件名判断相当equals/equalsOnSystem/equalsNormalized/equalsNormalizedOnSystem等操作。

二、源码说明

1.FilenameUtils类package org.apache.commons.io;@b@@b@import java.io.File;@b@import java.io.IOException;@b@import java.util.ArrayList;@b@import java.util.Collection;@b@import java.util.Stack;@b@@b@public class FilenameUtils@b@{@b@  public static final char EXTENSION_SEPARATOR = 46;@b@  public static final String EXTENSION_SEPARATOR_STR = Character.toString('.');@b@  private static final char UNIX_SEPARATOR = 47;@b@  private static final char WINDOWS_SEPARATOR = 92;@b@  private static final char SYSTEM_SEPARATOR = File.separatorChar;@b@  private static final char OTHER_SEPARATOR;@b@@b@  static boolean isSystemWindows()@b@  {@b@    return (SYSTEM_SEPARATOR == '\\');@b@  }@b@@b@  private static boolean isSeparator(char ch)@b@  {@b@    return ((ch == '/') || (ch == '\\'));@b@  }@b@@b@  public static String normalize(String filename)@b@  {@b@    return doNormalize(filename, SYSTEM_SEPARATOR, true);@b@  }@b@@b@  public static String normalize(String filename, boolean unixSeparator)@b@  {@b@    char separator = (unixSeparator) ? '/' : '\\';@b@    return doNormalize(filename, separator, true);@b@  }@b@@b@  public static String normalizeNoEndSeparator(String filename)@b@  {@b@    return doNormalize(filename, SYSTEM_SEPARATOR, false);@b@  }@b@@b@  public static String normalizeNoEndSeparator(String filename, boolean unixSeparator)@b@  {@b@    char separator = (unixSeparator) ? '/' : '\\';@b@    return doNormalize(filename, separator, false);@b@  }@b@@b@  private static String doNormalize(String filename, char separator, boolean keepSeparator)@b@  {@b@    if (filename == null)@b@      return null;@b@@b@    int size = filename.length();@b@    if (size == 0)@b@      return filename;@b@@b@    int prefix = getPrefixLength(filename);@b@    if (prefix = prefix; --j)@b@          if (array[j] == separator)@b@          {@b@            System.arraycopy(array, i + 1, array, j + 1, size - i);@b@            size -= i - j;@b@            i = j + 1;@b@            break label464:@b@          }@b@@b@@b@        System.arraycopy(array, i + 1, array, prefix, size - i);@b@        size -= i + 1 - prefix;@b@        i = prefix + 1;@b@      }@b@@b@@b@    if (size <= 0)@b@      label464: return "";@b@@b@    if (size <= prefix)@b@      return new String(array, 0, size);@b@@b@    if ((lastIsDirectory) && (keepSeparator))@b@      return new String(array, 0, size);@b@@b@    return new String(array, 0, size - 1);@b@  }@b@@b@  public static String concat(String basePath, String fullFilenameToAdd)@b@  {@b@    int prefix = getPrefixLength(fullFilenameToAdd);@b@    if (prefix  0)@b@      return normalize(fullFilenameToAdd);@b@@b@    if (basePath == null)@b@      return null;@b@@b@    int len = basePath.length();@b@    if (len == 0)@b@      return normalize(fullFilenameToAdd);@b@@b@    char ch = basePath.charAt(len - 1);@b@    if (isSeparator(ch))@b@      return normalize(new StringBuilder().append(basePath).append(fullFilenameToAdd).toString());@b@@b@    return normalize(new StringBuilder().append(basePath).append('/').append(fullFilenameToAdd).toString());@b@  }@b@@b@  public static boolean directoryContains(String canonicalParent, String canonicalChild)@b@    throws IOException@b@  {@b@    if (canonicalParent == null) {@b@      throw new IllegalArgumentException("Directory must not be null");@b@    }@b@@b@    if (canonicalChild == null) {@b@      return false;@b@    }@b@@b@    if (IOCase.SYSTEM.checkEquals(canonicalParent, canonicalChild)) {@b@      return false;@b@    }@b@@b@    return IOCase.SYSTEM.checkStartsWith(canonicalChild, canonicalParent);@b@  }@b@@b@  public static String separatorsToUnix(String path)@b@  {@b@    if ((path == null) || (path.indexOf(92) == -1))@b@      return path;@b@@b@    return path.replace('\\', '/');@b@  }@b@@b@  public static String separatorsToWindows(String path)@b@  {@b@    if ((path == null) || (path.indexOf(47) == -1))@b@      return path;@b@@b@    return path.replace('/', '\\');@b@  }@b@@b@  public static String separatorsToSystem(String path)@b@  {@b@    if (path == null)@b@      return null;@b@@b@    if (isSystemWindows())@b@      return separatorsToWindows(path);@b@@b@    return separatorsToUnix(path);@b@  }@b@@b@  public static int getPrefixLength(String filename)@b@  {@b@    if (filename == null)@b@      return -1;@b@@b@    int len = filename.length();@b@    if (len == 0)@b@      return 0;@b@@b@    char ch0 = filename.charAt(0);@b@    if (ch0 == ':')@b@      return -1;@b@@b@    if (len == 1) {@b@      if (ch0 == '~')@b@        return 2;@b@@b@      return ((isSeparator(ch0)) ? 1 : 0);@b@    }@b@    if (ch0 == '~') {@b@      int posUnix = filename.indexOf(47, 1);@b@      int posWin = filename.indexOf(92, 1);@b@      if ((posUnix == -1) && (posWin == -1))@b@        return (len + 1);@b@@b@      posUnix = (posUnix == -1) ? posWin : posUnix;@b@      posWin = (posWin == -1) ? posUnix : posWin;@b@      return (Math.min(posUnix, posWin) + 1);@b@    }@b@    char ch1 = filename.charAt(1);@b@    if (ch1 == ':') {@b@      ch0 = Character.toUpperCase(ch0);@b@      if ((ch0 >= 'A') && (ch0 <= 'Z')) {@b@        if ((len == 2) || (!(isSeparator(filename.charAt(2)))))@b@          return 2;@b@@b@        return 3;@b@      }@b@      return -1;@b@    }@b@    if ((isSeparator(ch0)) && (isSeparator(ch1))) {@b@      int posUnix = filename.indexOf(47, 2);@b@      int posWin = filename.indexOf(92, 2);@b@      if (((posUnix == -1) && (posWin == -1)) || (posUnix == 2) || (posWin == 2))@b@        return -1;@b@@b@      posUnix = (posUnix == -1) ? posWin : posUnix;@b@      posWin = (posWin == -1) ? posUnix : posWin;@b@      return (Math.min(posUnix, posWin) + 1);@b@    }@b@    return ((isSeparator(ch0)) ? 1 : 0);@b@  }@b@@b@  public static int indexOfLastSeparator(String filename)@b@  {@b@    if (filename == null)@b@      return -1;@b@@b@    int lastUnixPos = filename.lastIndexOf(47);@b@    int lastWindowsPos = filename.lastIndexOf(92);@b@    return Math.max(lastUnixPos, lastWindowsPos);@b@  }@b@@b@  public static int indexOfExtension(String filename)@b@  {@b@    if (filename == null)@b@      return -1;@b@@b@    int extensionPos = filename.lastIndexOf(46);@b@    int lastSeparator = indexOfLastSeparator(filename);@b@    return ((lastSeparator > extensionPos) ? -1 : extensionPos);@b@  }@b@@b@  public static String getPrefix(String filename)@b@  {@b@    if (filename == null)@b@      return null;@b@@b@    int len = getPrefixLength(filename);@b@    if (len  filename.length())@b@      return new StringBuilder().append(filename).append('/').toString();@b@@b@    return filename.substring(0, len);@b@  }@b@@b@  public static String getPath(String filename)@b@  {@b@    return doGetPath(filename, 1);@b@  }@b@@b@  public static String getPathNoEndSeparator(String filename)@b@  {@b@    return doGetPath(filename, 0);@b@  }@b@@b@  private static String doGetPath(String filename, int separatorAdd)@b@  {@b@    if (filename == null)@b@      return null;@b@@b@    int prefix = getPrefixLength(filename);@b@    if (prefix = filename.length()) || (index = endIndex))@b@      return "";@b@@b@    return filename.substring(prefix, endIndex);@b@  }@b@@b@  public static String getFullPath(String filename)@b@  {@b@    return doGetFullPath(filename, true);@b@  }@b@@b@  public static String getFullPathNoEndSeparator(String filename)@b@  {@b@    return doGetFullPath(filename, false);@b@  }@b@@b@  private static String doGetFullPath(String filename, boolean includeSeparator)@b@  {@b@    if (filename == null)@b@      return null;@b@@b@    int prefix = getPrefixLength(filename);@b@    if (prefix = filename.length()) {@b@      if (includeSeparator)@b@        return getPrefix(filename);@b@@b@      return filename;@b@    }@b@@b@    int index = indexOfLastSeparator(filename);@b@    if (index  extensions)@b@  {@b@    if (filename == null)@b@      return false;@b@@b@    if ((extensions == null) || (extensions.isEmpty()))@b@      return (indexOfExtension(filename) == -1);@b@@b@    String fileExt = getExtension(filename);@b@    for (String extension : extensions)@b@      if (fileExt.equals(extension))@b@        return true;@b@@b@@b@    return false;@b@  }@b@@b@  public static boolean wildcardMatch(String filename, String wildcardMatcher)@b@  {@b@    return wildcardMatch(filename, wildcardMatcher, IOCase.SENSITIVE);@b@  }@b@@b@  public static boolean wildcardMatchOnSystem(String filename, String wildcardMatcher)@b@  {@b@    return wildcardMatch(filename, wildcardMatcher, IOCase.SYSTEM);@b@  }@b@@b@  public static boolean wildcardMatch(String filename, String wildcardMatcher, IOCase caseSensitivity)@b@  {@b@    if ((filename == null) && (wildcardMatcher == null))@b@      return true;@b@@b@    if ((filename == null) || (wildcardMatcher == null))@b@      return false;@b@@b@    if (caseSensitivity == null)@b@      caseSensitivity = IOCase.SENSITIVE;@b@@b@    String[] wcs = splitOnTokens(wildcardMatcher);@b@    boolean anyChars = false;@b@    int textIdx = 0;@b@    int wcsIdx = 0;@b@    Stack backtrack = new Stack();@b@    do@b@    {@b@      if (backtrack.size() > 0) {@b@        int[] array = (int[])backtrack.pop();@b@        wcsIdx = array[0];@b@        textIdx = array[1];@b@        anyChars = true;@b@      }@b@@b@      while (wcsIdx  filename.length())@b@            break;@b@@b@          anyChars = false;@b@        }@b@        else if (wcs[wcsIdx].equals("*"))@b@        {@b@          anyChars = true;@b@          if (wcsIdx == wcs.length - 1)@b@            textIdx = filename.length();@b@@b@        }@b@        else@b@        {@b@          if (anyChars)@b@          {@b@            textIdx = caseSensitivity.checkIndexOf(filename, textIdx, wcs[wcsIdx]);@b@            if (textIdx == -1)@b@            {@b@              break;@b@            }@b@            int repeat = caseSensitivity.checkIndexOf(filename, textIdx + 1, wcs[wcsIdx]);@b@            if (repeat >= 0) {@b@              backtrack.push(new int[] { wcsIdx, repeat });@b@            }@b@@b@          }@b@          else if (!(caseSensitivity.checkRegionMatches(filename, textIdx, wcs[wcsIdx])))@b@          {@b@            break;@b@          }@b@@b@          textIdx += wcs[wcsIdx].length();@b@          anyChars = false;@b@        }@b@@b@        ++wcsIdx;@b@      }@b@@b@      if ((wcsIdx == wcs.length) && (textIdx == filename.length()))@b@        return true;@b@    }@b@@b@    while (backtrack.size() > 0);@b@@b@    return false;@b@  }@b@@b@  static String[] splitOnTokens(String text)@b@  {@b@    if ((text.indexOf(63) == -1) && (text.indexOf(42) == -1)) {@b@      return new String[] { text };@b@    }@b@@b@    char[] array = text.toCharArray();@b@    ArrayList list = new ArrayList();@b@    StringBuilder buffer = new StringBuilder();@b@    for (int i = 0; i  0) && (!(((String)list.get(list.size() - 1)).equals("*")))))@b@        {@b@          list.add("*");@b@        }@b@      } else {@b@        buffer.append(array[i]);@b@      }@b@@b@    if (buffer.length() != 0) {@b@      list.add(buffer.toString());@b@    }@b@@b@    return ((String[])list.toArray(new String[list.size()]));@b@  }@b@@b@  static@b@  {@b@    if (isSystemWindows())@b@      OTHER_SEPARATOR = '/';@b@    else@b@      OTHER_SEPARATOR = '\\';@b@  }@b@}

2.IOCase类package org.apache.commons.io;@b@@b@import java.io.Serializable;@b@@b@public final class IOCase@b@  implements Serializable@b@{@b@  public static final IOCase SENSITIVE = new IOCase("Sensitive", true);@b@  public static final IOCase INSENSITIVE = new IOCase("Insensitive", false);@b@  public static final IOCase SYSTEM = new IOCase("System", false);@b@  private static final long serialVersionUID = -6343169151696340687L;@b@  private final String name;@b@  private final transient boolean sensitive;@b@@b@  public static IOCase forName(String name)@b@  {@b@    if (SENSITIVE.name.equals(name))@b@      return SENSITIVE;@b@@b@    if (INSENSITIVE.name.equals(name))@b@      return INSENSITIVE;@b@@b@    if (SYSTEM.name.equals(name))@b@      return SYSTEM;@b@@b@    throw new IllegalArgumentException("Invalid IOCase name: " + name);@b@  }@b@@b@  private IOCase(String name, boolean sensitive)@b@  {@b@    this.name = name;@b@    this.sensitive = sensitive;@b@  }@b@@b@  private Object readResolve()@b@  {@b@    return forName(this.name);@b@  }@b@@b@  public String getName()@b@  {@b@    return this.name;@b@  }@b@@b@  public boolean isCaseSensitive()@b@  {@b@    return this.sensitive;@b@  }@b@@b@  public int checkCompareTo(String str1, String str2)@b@  {@b@    if ((str1 == null) || (str2 == null))@b@      throw new NullPointerException("The strings must not be null");@b@@b@    return ((this.sensitive) ? str1.compareTo(str2) : str1.compareToIgnoreCase(str2));@b@  }@b@@b@  public boolean checkEquals(String str1, String str2)@b@  {@b@    if ((str1 == null) || (str2 == null))@b@      throw new NullPointerException("The strings must not be null");@b@@b@    return ((this.sensitive) ? str1.equals(str2) : str1.equalsIgnoreCase(str2));@b@  }@b@@b@  public boolean checkStartsWith(String str, String start)@b@  {@b@    return str.regionMatches((!(this.sensitive)) ? 1 : false, 0, start, 0, start.length());@b@  }@b@@b@  public boolean checkEndsWith(String str, String end)@b@  {@b@    int endLen = end.length();@b@    return str.regionMatches((!(this.sensitive)) ? 1 : false, str.length() - endLen, end, 0, endLen);@b@  }@b@@b@  public int checkIndexOf(String str, int strStartIndex, String search)@b@  {@b@    int i;@b@    int endIndex = str.length() - search.length();@b@    if (endIndex >= strStartIndex)@b@      for (i = strStartIndex; i <= endIndex; ++i)@b@        if (checkRegionMatches(str, i, search))@b@          return i;@b@@b@@b@@b@    return -1;@b@  }@b@@b@  public boolean checkRegionMatches(String str, int strStartIndex, String search)@b@  {@b@    return str.regionMatches((!(this.sensitive)) ? 1 : false, strStartIndex, search, 0, search.length());@b@  }@b@@b@  public String toString()@b@  {@b@    return this.name;@b@  }@b@}

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_30490729/article/details/114232539

智能推荐

Android4150数字量,C#获取ADAM4150数字量-程序员宅基地

【实例简介】【实例截图】【核心代码】using DigitalLibrary;using System;using System.Collections.Generic;using System.IO.Ports;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using S..._c# 数字量输入测试

PDF文件怎么修改,PDF页眉页脚怎么设置-程序员宅基地

  我们有是需要操作编辑PDF文件,当我们需要给PDF文件设置页眉页脚的时候要怎么操作呢,不会的小伙伴可以跟小编一起来看看下面的文章了解一下哦。   1.打开运行PDF编辑器,在编辑器中打开需要修改的PDF文件。   2.打开文件后,选择编辑器中菜单栏里的文档,然后选择文档中的页眉..._mathcad 生成pdf页眉怎么修改

electron中自定义菜单栏_electron 菜单栏-程序员宅基地

2、默认情况下, 无边框窗口是不可拖拽的。应用程序需要在 CSS 中指定 -webkit-app-region: drag 来告诉 Electron 哪些区域是可拖拽的(如操作系统的标准标题栏),在可拖拽区域内部使用 -webkit-app-region: no-drag 则可以将其中部分区域排除。请注意, 当前只支持矩形形状。1、设置browerWindow的autoHideMenuBar为true隐藏菜单栏。_electron 菜单栏

ROS学习笔记之导航实现01_SLAM建图_ros slam建图-程序员宅基地

准备工作请先安装相关的ROS功能包: 安装 gmapping 包(用于构建地图):sudo apt install ros-<ROS版本>-gmapping 安装地图服务包(用于保存与读取地图):sudo apt install ros-<ROS版本>-map-server 安装 navigation 包(用于定位以及路径规划):sudo apt install ros-<ROS版本>-navigation 新建功能包,并导入依赖: gm_ros slam建图

Vite 开发快速入门_猫老板的豆的博客-程序员宅基地

文章目录Vite 简介优点缺点相关文献Vite 安装Vue3中使用jsx引入插件注册插件使用插件Vite 简介Vite (法语意为 “快速的”,发音 /vit/) 是一种新型前端构建工具,能够显著提升前端开发体验。它主要由两部分组成:一个开发服务器,它基于 原生 ES 模块 提供了 丰富的内建功能,如速度快到惊人的 模块热更新(HMR)。一套构建指令,它使用 Rollup 打包你的代码,并且它是预配置的,可输出用于生产环境的高度优化过的静态资源。Vite 意在提供开箱即用的配置,同时它的 .

随便推点

win10配置ftp服务,供局域网内共享文件-程序员宅基地

新建share文件夹,右键属性、共享选项卡中点击共享,此时还无法访问控制面板》》》程序或功能》》》启用或关闭windows功能》》》FTP服务器还有IIS管理控制台选中确定保存搜索栏搜索IIS,打开管理器右键网站,添加FTP站点,名称可随意,物理路径选择我们第一新建好的share文件夹,下一步IP地址要用本地地址,端口号21,虚拟主机随意,可要可不要,要的话,无非是访问时可以换一种输入罢了这里根据自身需要吧...

VC 6.0调试 之 DLL调试-程序员宅基地

你写的DLL或者ActiveX需要调试,因为他们不能直接运行 ,所以不能够直接调试,那么可以试试下面的方法:1. 在Vc 6.0中新建一个Win32 Console Application工程(例如DLLTest),代码中调用需要调试的DLL,编译该工程,生成Debug/DLLTest.exe,关闭该工程。2. 打开VC 6.0,打开需要调试的DLL工程,点击菜单Build->Set Active

探索platform.xml ---- 入门学习下-程序员宅基地

探索platform.xmlhttps://blog.csdn.net/sinat_20059415/article/details/80464243?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task前言:最近工作涉及到platform.x..._platform.xml

poj-3237-鸡兔同笼-C语言-简单计算_用c语言求鸡兔同笼-程序员宅基地

C语言入门最简单实例,学会将一个用自然语言描述的实际问题抽象成一个计算问题,给出计算过程,继而编程实现计算过程,并将计算结果还原成对原来问题的解答。#include int main(){ int n,m; scanf("%d",&n); for(;n>0;n--) { scanf("%d",&m); if(m%2) _用c语言求鸡兔同笼

Linux嵌入式操作系统移植及启动概述_嵌入式系统怎么移植操作系统作用是啥?-程序员宅基地

  如果就“Linux嵌入式操作系统移植”打个比喻,那么“Linux嵌入式操作系统移植”就像是给“PC机装Windows操作系统”。第一步:Bootloader移植(类比于设置PC机中的Bios)1、BootLoader作用:①检查并初始化硬件;②引导加载Linux内核。2、BootLoader介绍  不仅仅在Linux嵌入式操作系统中存在BootLoader,在其他嵌入式操作系统中,如V..._嵌入式系统怎么移植操作系统作用是啥?

qt实现自定义菜单_qt自定义菜单栏框架_我是标同学的博客-程序员宅基地

这个函数中快乐的操作啦。//注意:让对方发射信号后对方就没法执行它默认的菜单动作了,所以我们得完全在我们的槽函数中自己生成好菜单。_qt自定义菜单栏框架