public class Path
extends java.lang.Object
Directory
or File
item. Some examples:
/ | The root diretory |
/filename | A file in the root diretory |
/directory | A directory in root diretory |
/directory/filename | A file in a directory |
/directory/subdirectory | A subdirectory |
/directory/subdirectory/filename | A file in a subdirectory |
~ | The currently logged in user's home directory |
~/filename | A file in the current user's home directory |
~/directory | A directory in the current user's home directory |
~login | The home directory of the user with the specified login |
~login/filename | A file in the home directory of the user with the specified login |
~login/directory | A directory in the home directory of the user with the specified login |
Modifier and Type | Class and Description |
---|---|
static class |
Path.Type
An enum that describes what type the path is
|
Modifier and Type | Field and Description |
---|---|
private java.lang.String[] |
directories
The directory part of the path.
|
private java.lang.String |
filename
The filename part of the path.
|
private static java.util.regex.Pattern |
invalid
A regexp checking for invalid characters.
|
static java.lang.String |
INVALID_CHARACTERS
These characters are not valid within a file or directory name:
"~\\/:;*?<>|\""
|
private Path.Type |
type
The type of the path.
|
private java.lang.String |
userlogin
The userlogin part of the path.
|
Constructor and Description |
---|
Path(Path directoryPath,
java.lang.String filename)
Create a new
Path object. |
Path(java.lang.String path,
Path.Type type)
Create a new
Path object by parsing the given string. |
Path(java.lang.String userlogin,
java.lang.String[] directories,
java.lang.String filename)
Create a new
Path object. |
Modifier and Type | Method and Description |
---|---|
java.lang.String |
getDirectory(int i)
Get the name of the i:th directory on the path, starting with 0.
|
int |
getDirectoryCount()
Get the number of directories on the path.
|
java.lang.String |
getFilename()
Get the filename part of the path.
|
Path |
getParent()
Get the parent path.
|
Path.Type |
getType()
Get the type of the path.
|
java.lang.String |
getUserLogin()
Get the userlogin part of the path.
|
static boolean |
isValidName(java.lang.String name)
Check if a name is valid as a partial path name, ie. it must not contain any
of the characters in
INVALID_CHARACTERS . |
static java.lang.String |
makeSafeFilename(java.lang.String originalName,
java.lang.String replaceWith)
Convert a string to a name that is safe to use as a filename.
|
java.lang.String |
toString()
Get the string representation of the path.
|
java.lang.String |
toURLString(java.lang.String enc)
Get this Path's URL-string encoded with a given
character encoding.
|
public static final java.lang.String INVALID_CHARACTERS
private static final java.util.regex.Pattern invalid
private Path.Type type
private java.lang.String userlogin
private java.lang.String[] directories
private java.lang.String filename
public Path(java.lang.String path, Path.Type type) throws InvalidPathException
Path
object by parsing the given string.path
- The string representation of a pathtype
- If the path represents a Path.Type.FILE
or
Path.Type.DIRECTORY
InvalidPathException
- If the path is not validPath(java.lang.String userlogin, java.lang.String[] directories, java.lang.String filename)
Path
object.userlogin
- The userlogin part of the pathdirectories
- The directories in the pathfilename
- The filename part of the pathPath(Path directoryPath, java.lang.String filename)
Path
object.directoryPath
- The directory pathfilename
- The filename part of the pathpublic static boolean isValidName(java.lang.String name)
INVALID_CHARACTERS
.name
- The string to be tested.public static java.lang.String makeSafeFilename(java.lang.String originalName, java.lang.String replaceWith)
INVALID_CHARACTERS
charactes with the specified string.originalName
- The original namereplaceWith
- The string to replace all unsafe characters withpublic Path getParent()
public java.lang.String getUserLogin()
public int getDirectoryCount()
public java.lang.String getDirectory(int i)
i
- Should be between 0 and getDirectoryCount-1
public java.lang.String getFilename()
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String toURLString(java.lang.String enc) throws java.io.UnsupportedEncodingException
enc
- The encoding to use. UTF-8 is used is this is null.java.io.UnsupportedEncodingException
- If the give encoding is not supported