Created
November 9, 2018 11:52
-
-
Save awwsmm/002c099e68265b8a171980a4038adb6e to your computer and use it in GitHub Desktop.
Parse any file name with Java and get path, name, and extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.IOException; | |
import java.util.Arrays; | |
import java.util.Optional; | |
public class ParseFileName { | |
public static void main (String[] args) { | |
System.out.println(Arrays.toString(parseFileName("test").get())); | |
System.out.println(Arrays.toString(parseFileName("test.a").get())); | |
System.out.println(Arrays.toString(parseFileName("test.a.b").get())); | |
System.out.println(Arrays.toString(parseFileName(".test").get())); | |
System.out.println(Arrays.toString(parseFileName(".test.a").get())); | |
System.out.println(Arrays.toString(parseFileName(".test.a.b").get())); | |
System.out.println(Arrays.toString(parseFileName("a.b/.test").get())); | |
System.out.println(Arrays.toString(parseFileName("a.b/.test.a").get())); | |
System.out.println(Arrays.toString(parseFileName("a.b/.test.a.b").get())); | |
System.out.println(Arrays.toString(parseFileName("a.b/..test").get())); | |
System.out.println(Arrays.toString(parseFileName("a.b/..test..a").get())); | |
System.out.println(Arrays.toString(parseFileName("a.b/..test..a..b").get())); | |
} | |
public static Optional<String[]> parseFileName (String fileName) { | |
// if fileName is null or empty, return empty Optional | |
if (fileName == null || fileName.trim().equals("")) return Optional.empty(); | |
// get the full path from the user-supplied file name | |
// if ~ appears at the beginning of the path, replace with user's home directory | |
File file = new File(fileName.trim().replaceFirst("^~",System.getProperty("user.home"))); | |
String canonicalPath = ""; | |
// try to get the canonical path of the file | |
try { canonicalPath = file.getCanonicalPath(); | |
} catch (SecurityException ex) { | |
System.err.println("parseFileName() : SecurityException"); | |
return Optional.empty(); | |
} catch (IOException ex) { | |
System.err.println("parseFileName() : IOException"); | |
return Optional.empty(); | |
} | |
// all file names contain at least one slash -- get the last one | |
int lastSlash = Math.max(canonicalPath.lastIndexOf('\\'), canonicalPath.lastIndexOf('/')); | |
// use last slash to split path and filename -- make sure path is not an empty string | |
// otherwise, remove trailing slash from path: | |
// "C:" not "C:\" for Windows; but "/" for Linux root directory, not "" | |
String pathOnly = canonicalPath.substring(0, Math.max(lastSlash, 1)); | |
String nameOnly = canonicalPath.substring(lastSlash+1, canonicalPath.length()); | |
String extnOnly = ""; | |
// Linux: hidden files ("dotfiles") begin with '.' | |
// Mac: "resource fork" files begin with "._" (safe to delete / ignore) | |
// Windows: temporary "lock" files begin with '~' or "~$" | |
boolean dotfile = false; | |
int nLeadingDots = 0; | |
// if file name begins with '.', replace this dot, don't confuse with extension | |
// ex: /home/user/.gitignore : file name is ".gitignore", no extension | |
// ex: /home/user/.thing.tar.gz : file name is ".thing", extension is ".tar.gz" | |
// special treatment for "dotfiles" | |
while (nameOnly.charAt(nLeadingDots) == '.') | |
++nLeadingDots; | |
// check if the file has an extension (it might not) | |
int dot = -1; | |
if ((dot = nameOnly.indexOf('.', nLeadingDots)) > 0) { | |
extnOnly = nameOnly.substring(dot+1); | |
nameOnly = nameOnly.substring(0, dot); | |
} | |
// trim() to remove any leading / trailing spaces | |
return Optional.of(new String[]{ pathOnly.trim(), nameOnly.trim(), extnOnly.trim() }); | |
} // end parseFileName() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment