技术标签: 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@}
文章浏览阅读65次。synchronizedsynchronized同步代码块同步函数两种方式。/*死锁,共享资源自有自己的锁,然后相互访问都要解锁,如果相互拿不到对方的锁,可能产生死锁synchronized例子说明:两个锁中锁,A和B,然后A有资源,然后B锁在A中锁了一个资源。然后两个线程来访问。引起死锁。在开发中要避免死锁。*/classLockTest implements Runnable{private ..._oracle两个线程同时锁一张表
文章浏览阅读355次。一、键盘分为那几个部分: 1.功能区 2.指示灯区 3.打字键盘区 4.编辑区 5.数字键盘区 二、规范打字: 1.食指:(左边)45 rt fg vb (右边)67 yu hj nm 2.中指:(左边)3edc (右边)8ik, 3.无名指:(左边)2w..._鼠标复制英文加数字 site:blog.csdn.net
文章浏览阅读993次。Pico汽车诊断示波器把你的PC电脑变成一台功能强大的汽车诊断工具,快速查找现代车辆上日益增加的传感器,执行器和电路的故障。12位高分辨率的PC示波器模块连接到你的电脑的USB接口上,并且能够采集达250M样本,使它能够捕捉复杂的汽车波形,包括CAN总线和FlexRay信号;然后放大可疑区域。由于基于PC,这些波形可以存储到你的PC电脑内用于以后参考,打印或发e-mail。强大的、容易使用的PicoScope汽车软件。整套套装里的一个重要组件就是功能强大的PicoScope汽车诊断软件。使用Pico_pq180
文章浏览阅读2.2w次,点赞4次,收藏9次。首先我们有一个对象类package com.xuzihui.ref;public class Student { public Student() { } private int id; private String name; private int math; private int english; public int getId() { retur..._java 通过对象 反射获取 对象的所有字段值
文章浏览阅读334次。数据缓存官方描述https://developers.weixin.qq.com/miniprogram/dev/api/storage/wx.setStorageSync.html每个微信小程序都可以有自己的本地缓存,可以通过wx.setStorage/wx.setStorageSync、wx.getStorage/wx.getStorageSync、wx.clearStorage/w..._微信小程序监控缓存内容
文章浏览阅读345次。By Recognizing These Guys, We Find Social Networks UsefulTime Limit: 2000/1000 MS (Java/Others)Memory Limit: 125536/65536 K (Java/Others)Total Submission(s): 4200Accepted Submission(s): 108..._hduby
文章浏览阅读1.2k次,点赞3次,收藏8次。异构计算技术分析_异构计算对神经网络的影响
文章浏览阅读2.4w次,点赞2次,收藏4次。使用VSport.dll 开发了虚拟串口工具 在开发电脑上 安装好驱动后 可以运行 正常创建虚拟串口 Virtual Serial Port ActiveX提供 vsport注册码,该注册码可以正常注册使用,注册码没有任何限制,但仅供测试使用,谢谢virtual Serial Port ActiveX Control Installation Here is a step-by-step inst..._virtual serial port driver注册码
文章浏览阅读1.2k次,点赞2次,收藏14次。抄袭:大佬openssl下载和qt例程:链接:https://pan.baidu.com/s/15avdzMeHgmB1qiWXB63Qow提取码:ulja代码:.pro文件添加了外部库#-------------------------------------------------## Project created by QtCreator 2022-04-14T10:20:16##---------------------------------------------._qt openssl windows
文章浏览阅读5.5k次。springboot 解析前端传来的时间字符串后端LocalDateTime解析失败 could not be parsed, unparsed text found at index 10_could not be parsed, unparsed text found at index 10
文章浏览阅读511次。官方文档:https://developers.google.com/web/tools/chrome-devtools/network/最近打算写一写Chrome教程文档,不知道大家最感兴趣的是什么内容呢?1.如何打开无论是在Windows还是Mac,都可以使用(FN)+F12键打开Chrome的Network面板。2.面板组成如图所示,Chrome的Network面板主要由5个部..._chrom network选择多个类型
文章浏览阅读156次。kafka面试基础[17]1.Kafka的用途有哪些?使用场景如何?2.Kafka中的ISR、AR又代表什么?ISR的伸缩又指什么3.Kafka中的HW、LEO、LSO、LW等分别代表什么?4.Kafka中是怎么体现消息顺序性的?5.Kafka中的分区器、序列化器、拦截器是否了解?它们之间的处理顺序是什么?6.Kafka生产者客户端的整体结构是什么样子的?7.Kafka生产者客户端中使用了几个线程来处理?分别是什么?8.Kafka的旧版Scala的消费者客户端的设计有什么缺陷?9.“消费组