Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Super full File tool Class FileUtil

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

< 0) { nPos = _sFilePathName.lastIndexOf('\\'); } return (nPos >

= 0? _ sFilePathName.substring (0, nPos + 1): ");} / * * extract the file name (including extension) from the full pathname (path + file name) of the file

* for example: d:\ path\ file.ext-> file.ext * * @ param _ sFilePathName * @ return * / public static String extractFileName (String _ sFilePathName) {return extractFileName (_ sFilePathName, File.separator);} / * extract the file name (including extension) from the full pathname (path + file name) of the file

* for example: d:\ path\ file.ext-- > file.ext * * @ param _ sFilePathName full file path name * @ param _ sFileSeparator file separator * @ return * / public static String extractFileName (String _ sFilePathName, String _ sFileSeparator) {int nPos =-1; if (_ sFileSeparator = = null) {nPos = _ sFilePathName.lastIndexOf (File.separatorChar); if (nPos)

< 0) { nPos = _sFilePathName .lastIndexOf(File.separatorChar == '/' ? '\\' : '/'); } } else { nPos = _sFilePathName.lastIndexOf(_sFileSeparator); } if (nPos < 0) { return _sFilePathName; } return _sFilePathName.substring(nPos + 1); } /** * 检查指定文件的路径是否存在 * * @param _sPathFileName 文件名称(含路径) * @return 若存在,则返回true;否则,返回false */ public static boolean pathExists(String _sPathFileName) { String sPath = extractFilePath(_sPathFileName); return fileExists(sPath); } public static boolean fileExists(String _sPathFileName) { File file = new File(_sPathFileName); return file.exists(); } /** * 创建目录 * * @param _sDir 目录名称 * @param _bCreateParentDir 如果父目录不存在,是否创建父目录 * @return */ public static boolean makeDir(String _sDir, boolean _bCreateParentDir) { boolean zResult = false; File file = new File(_sDir); if (_bCreateParentDir) zResult = file.mkdirs(); // 如果父目录不存在,则创建所有必需的父目录 else zResult = file.mkdir(); // 如果父目录不存在,不做处理 if (!zResult) zResult = file.exists(); return zResult; } /** * 移除字符串中的BOM前缀 * * @param _sLine 需要处理的字符串 * @return 移除BOM后的字符串. */ private static String removeBomHeaderIfExists(String _sLine) { if (_sLine == null) { return null; } String line = _sLine; if (line.length() >

< extenPosi ? filePath.substring(filePosi + 1, extenPosi) : filePath.substring(filePosi + 1)); } /** * get file name from path, include suffix * * * getFileName(null) = null * getFileName("") = "" * getFileName(" ") = " " * getFileName("a.mp3") = "a.mp3" * getFileName("a.b.rmvb") = "a.b.rmvb" * getFileName("abc") = "abc" * getFileName("c:\\") = "" * getFileName("c:\\a") = "a" * getFileName("c:\\a.b") = "a.b" * getFileName("c:a.txt\\a") = "a" * getFileName("/home/admin") = "admin" * getFileName("/home/admin/a.txt/b.mp3") = "b.mp3" * * * @param filePath * @return file name from path, include suffix */ public static String getFileName(String filePath) { if (StringUtils.isEmpty(filePath)) { return filePath; } int filePosi = filePath.lastIndexOf(File.separator); return (filePosi == -1) ? filePath : filePath.substring(filePosi + 1); } /** * get folder name from path * * * getFolderName(null) = null * getFolderName("") = "" * getFolderName(" ") = "" * getFolderName("a.mp3") = "" * getFolderName("a.b.rmvb") = "" * getFolderName("abc") = "" * getFolderName("c:\\") = "c:" * getFolderName("c:\\a") = "c:" * getFolderName("c:\\a.b") = "c:" * getFolderName("c:a.txt\\a") = "c:a.txt" * getFolderName("c:a\\b\\c\\d.txt") = "c:a\\b\\c" * getFolderName("/home/admin") = "/home" * getFolderName("/home/admin/a.txt/b.mp3") = "/home/admin/a.txt" * * * @param filePath * @return */ public static String getFolderName(String filePath) { if (StringUtils.isEmpty(filePath)) { return filePath; } int filePosi = filePath.lastIndexOf(File.separator); return (filePosi == -1) ? "" : filePath.substring(0, filePosi); } /** * get suffix of file from path * * * getFileExtension(null) = "" * getFileExtension("") = "" * getFileExtension(" ") = " " * getFileExtension("a.mp3") = "mp3" * getFileExtension("a.b.rmvb") = "rmvb" * getFileExtension("abc") = "" * getFileExtension("c:\\") = "" * getFileExtension("c:\\a") = "" * getFileExtension("c:\\a.b") = "b" * getFileExtension("c:a.txt\\a") = "" * getFileExtension("/home/admin") = "" * getFileExtension("/home/admin/a.txt/b") = "" * getFileExtension("/home/admin/a.txt/b.mp3") = "mp3" * * * @param filePath * @return */ public static String getFileExtension(String filePath) { if (StringUtils.isBlank(filePath)) { return filePath; } int extenPosi = filePath.lastIndexOf(FILE_EXTENSION_SEPARATOR); int filePosi = filePath.lastIndexOf(File.separator); if (extenPosi == -1) { return ""; } return (filePosi >

= extenPosi)? ": filePath.substring (extenPosi + 1);} / * Creates the directory named by the trailing filename of this file, including the complete directory path required * to create this directory. * Attentions: * makeDirs ("C:\\ Users\\ Trinea") can only create users folder * makeFolder ("C:\\ Users\\ Trinea\") can create Trinea folder * @ param filePath * @ return true if the necessary directories have been created or the target directory already exists, false one of * the directories can not be created. * * if {@ link FileUtils#getFolderName (String)} return null, return false * if target directory already exists, return true * return {@ link File#makeFolder} * * / public static boolean makeDirs (String filePath) {String folderName = getFolderName (filePath); if (StringUtils.isEmpty (folderName)) {return false;} File folder = new File (folderName); return (folder.exists () & & folder.isDirectory ())? True: folder.mkdirs ();} / * * @ param filePath * @ return * @ see # makeDirs (String) * / public static boolean makeFolders (String filePath) {return makeDirs (filePath);} / * Indicates if this file represents a file on the underlying file system. * * @ param filePath * @ return * / public static boolean isFileExist (String filePath) {if (StringUtils.isBlank (filePath)) {return false;} File file = new File (filePath); return (file.exists () & & file.isFile ());} / * * Indicates if this file represents a directory on the underlying file system. * * @ param directoryPath * @ return * / public static boolean isFolderExist (String directoryPath) {if (StringUtils.isBlank (directoryPath)) {return false;} File dire = new File (directoryPath); return (dire.exists () & & dire.isDirectory ());} / * * delete file or directory * * if path is null or empty, return true * if path not exist, return true * if path exist, delete recursion. Return true * @ param path * @ return * / public static boolean deleteFile (String path) {if (StringUtils.isBlank (path)) {return true;} File file = new File (path); if (! file.exists ()) {return true;} if (file.isFile ()) {return file.delete ();} if (! file.isDirectory ()) {return false } for (File f: file.listFiles ()) {if (f.isFile ()) {f.delete ();} else if (f.isDirectory ()) {deleteFile (f.getAbsolutePath ());}} return file.delete () } / * * get file size * * if path is null or empty, return-1 * if path exist and it is a file, return file size, else return-1 * @ param path * @ return returns the length of this file in bytes. Returns-1 if the file does not exist. * / public static long getFileSize (String path) {if (StringUtils.isBlank (path)) {return-1;} File file = new File (path); return (file.exists () & & file.isFile ()? File.length ():-1);}} these are all the contents of the article "sample Analysis of the Super full document tool Class FileUtil". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report