|
Letzte
Bearbeitung dieses Dokuments: |
Code
Erklärungen
und Anwendungsbeispiele
Verwandte
Dokumentation
package
js_base.utilities;
import
java.io.File;
/**
*
* @author kurt(at)javascout[dot]biz
* @date
2007-08-12
*
* @description
* de:
*
Klasse mit statischen Methoden zum Untersuchen und Verändern
von Datei- und
* Verzeichnis-Namen
* en:
*
Class with static methods that provide services to inspect and
manipulate
* File- and Directory-Names.
*
* @change-log
*
when who why
*
--------------------------------------------------------
*
*/public
class
JSBS_FileAndDirectoryServices
{
/*
*
--------------------
* de:
* Methode zum Ermitteln
des Teils mit dem Dateityp; das ist jener Teil eines
*
Dateinamens hinter dem letzten Punkt (.).
* en:
*
Method to get the part with the file-type; that is the part of a
filename behind
* the last full stop (.). */
public
static
String
getFileType(String parmDirectoryAndFileName) {
/*
*
de:
* Prüfen, ob der übergebene Parameter gültig
ist; sonst eine leere Zeichenkette zurück liefern.
*
en:
* Check if the parameter is valid; otherwise return an
empty String. */
if
(parmDirectoryAndFileName
==
null
)
return
""
;
/*
*
de:
* Eine Zeichenkette für den Rückgabe-Wert
definieren und diesen mit einer 'getrimmten' Version
* (ohne
führende und nachfolgende Leerzeichen) der als Parameter
übergebenen Zeichenkette
* füllen.
*
en:
* Define a String for the Return-value and fill it with
a trimmed (without leading or trailing
* blanks)
version of the input-string passed as parameter. */
String
strReturnValue = parmDirectoryAndFileName.trim(
)
;
/*
*
de:
* Festlegen des Punktes (.) als Trenner zwischen
Dateityp und restlichem Dateinamen.
* en:
* Define
the full-stop (.) as separator between file-type and the remaining
part of the
* remaining part of the file-name.
*/
String
strFiletypeSeparator =
"."
;
/*
*
de:
* Untersuchen der Zeichenkette auf '.' am Ende der
Zeichenkette.
* Das ist notwendig um den Dateityp
herausfinden zu können.
* en:
* Inspect
for trailing '.' and remove them.
* This is needed to
identify the file-type. */
for
(;;)
{
if
(strReturnValue.endsWith(strFiletypeSeparator))
{
/* de:
*
Herauslösen des Teils der Zeichenkette ohne das letzte Zeichen
und
* innerhalb dieser for-Schleife noch einmal
untersuchen.
* en:
* Take the part of the string
without the last character
* and continue the for-loop
to do inspecting. */
strReturnValue
= strReturnValue.substring(0, strReturnValue.length() -
1
)
;
}/* de:
Keinen (weiteren) '.' gefunden; Schleife beenden.
* en: No
(more) trailing '.' found; end loop. */
else
break
;
}/*
*
de:
* Verarbeitung zum Herausfinden und Trennen des Teils
mit dem Dateinamen oder
* des Namens des untersten
Verzeichnisses beginnen.
* Nach der letzten Postion eines
'.' innerhalb der Zeichenkette suchen.
* en:
* Start
the process to identify and separate the part with the file-name or
the
* last directory-name.
* Search for
the last position of the file-separator within the string.
*/
int
intFiletypeSeparatorPosition
= strReturnValue.lastIndexOf(strFiletypeSeparator);
/*
*
de: Prüfen ob ein '.' innerhalb der Zeichenkette gefunden
wurde.
* en: See if there was '.' found within the whole
string. */
if
(intFiletypeSeparatorPosition
> 0) {
/*
*
de:
* '.' gefunden; Teil der Zeichenkette dahinter (ohne dem
'.') herauslösen.
* en:
* '.' found; extract
the sub-string before it (without the '.').
*/
strReturnValue
= strReturnValue.substring(intFiletypeSeparatorPosition +
1
)
;
}
else
{
/*
*
de:
* Kein '.' in der Zeichenkette gefunden; Dateityp nicht
identifizierbar.
* Leere Zeichenkette als Hinweis zurück
liefern.
* en:
* No '.' found within the string;
file-type can not be identified.
* Return an empty
string to signal this. */
/*
* de: Zeichenkette mit
dem Ergebnis zurück liefern.
* en: Return the String
with the result. */
strReturnValue
=
""
;
}
return
strReturnValue;
}/*
*
--------------------
* de:
* Methode zum Ermitteln
des Teils mit dem Dateinamen oder des Namen des letzten
*
Verzeichnisses in einer Zeichenkette mit dem gesamten oder letzten
Teil
* einer Verzeichnisstruktur.
* 'gesamt'
bedeutet, dass alle Verzeichnisnamen vom 'root'-Verzeichnis
abwärts
* in der Zeichenkette enthalten sind.
*
en:
* Method to get the part with the filename or the
last directory in a string
* containing a fully or partly
qualified File- or Directory-Name.
* 'Fully qualified'
means, that all directories from the root down are
contained
* within the string. */
public
static
String
getFileOrLastDirectory(String parmDirectoryAndFileName) {
/*
*
de:
* Prüfen, ob der übergebene Parameter gültig
ist; sonst eine leere Zeichenkette zurück liefern.
*
en:
* Check if the parameter is valid; otherwise return an
empty String. */
if
(parmDirectoryAndFileName
==
null
)
return
""
;
/*
*
de:
* Eine Zeichenkette für den Rückgabe-Wert
definieren und diesen mit einer 'getrimmten' Version
* (ohne
führende und nachfolgende Leerzeichen) der als Parameter
übergebenen Zeichenkette
* füllen.
*
en:
* Define a String for the Return-value and fill it with
a trimmed (without leading or trailing
* blanks)
version of the input-string passed as parameter. */
String
strReturnValue = parmDirectoryAndFileName.trim(
)
;
/*
*
de:
* Ermitteln des 'file-separator' des Betriebssystems;
das ist das Zeichen, das innerhalb einer
* Zeichenkette die
Namen der einzelnen Verzeichnisse (und den Dateinamen vom untersten
Verzeichnis)
* trennt.
* en:
* Get the
operating-system-specific file-separator; that is the character that
separates the
* directory-names (and the file-name from
the last directory) within the String with
* the
Directory- and File-Name. */
String
strFileSeparator = System.getProperty(
"file.separator")
;
/*
*
de:
* Untersuchen der Zeichenkette auf 'file-separators' am
Ende der Zeichenkette
* und diese entfernen. Das ist
notwendig um den Dateinamen oder den Namen des
* untersten
Verzeichnis herausfinden zu können.
* en:
* Inspect
for trailing 'file-separators' and remove them.
* This
is needed to identify the file-name or last directory-name.
*/
for
(;;)
{
if
(strReturnValue.endsWith(strFileSeparator))
{
/* de:
*
Herauslösen des Teils der Zeichenkette ohne das letzte Zeichen
und
* innerhalb dieser Schleife noch einmal untersuchen.
*
en:
* Take the part of the string without the last character
* and continue the for-loop to do inspecting.
*/
strReturnValue
= strReturnValue.substring(0, strReturnValue.length() -
1
)
;
}/* de:
Keinen (weiteren) 'file-separator' gefunden; Schleife beenden.
*
en: No (more) trailing 'file-separator' found; end loop. */
else
break
;
}/*
*
de:
* Verarbeitung zum Herausfinden und Trennen des Teils
mit dem Dateinamen oder
* des Namens des untersten
Verzeichnisses beginnen.
* Nach der letzten Postion eines
'.' innerhalb der Zeichenkette suchen.
* en:
* Start
the process to identify and separate the part with the file-name or
the
* last directory-name.
* Search for
the last position of the file-separator within the string.
*/
int
intFileSeparatorPosition
= strReturnValue.lastIndexOf(strFileSeparator);
/*
*
de: Prüfen ob ein 'file-separator' innerhalb der Zeichenkette
gefunden wurde.
* en: See if there was 'file-separator'
found within the whole string. */
if
(intFileSeparatorPosition
> 0) {
/*
*
de:
* 'File-separator' gefunden; Teil der Zeichenkette
dahinter (ohne dem 'file-separator')
* herauslösen.
*
en:
* 'File-Separator' found; extract the sub-string before
it (without the 'file-seperator'). */
strReturnValue
= strReturnValue.substring(intFileSeparatorPosition +
1
)
;
}/*
*
de:
* Keine Verarbeitung im 'else'-Zweig weil der
Rückgabe-Wert bereits den Dateinamen
* oder den Namen
des untersten Verzeichnisses enthält.
* en:
*
No processing in the 'else'-branch as the return-value already
contains a
* file-name or a single directory-name.
*/
/*
* de: Zeichenkette mit dem Ergebnis zurück
liefern.
* en: Return the String with the result.
*/
return
strReturnValue;
}/*
*
--------------------
* Method to remove the part with the
filename or the last directory in a string
* containing a
fully or partly qualified File- or Directory-Name.
* 'Fully
qualified' means, that all directories from the root down are
contained
* within the string. */
public
static
String
removeFileOrLastDirectory(String parmDirectoryAndFileName) {
/*
*
Check if the parameter is valid; otherwise return empty String.
*/
if
(parmDirectoryAndFileName
==
null
)
return
""
;
/*
*
Define a String for the Return-value and fill it with a trimmed
(without leading or trailing
* blanks) version of the
input-string. */
String
strReturnValue = parmDirectoryAndFileName.trim(
)
;
/*
*
Get the system-specific file-separator; that is the character that
separates the
* directory-names (and the file-name from
the last directory) within the String with
* the
Directory- and File-Name. */
String
strFileSeparator =
System.getProperty(
"file.separator")
;
/*
* Inspect
for trailing 'file-separators' and remove them.
* This
is needed to identify the file-name or last directory-name.
*/
for
(;;)
{
if
(strReturnValue.endsWith(strFileSeparator))
{
/* Take
the part of the string without the last character
* and
continue the for-loop to do inspecting. */
strReturnValue
= strReturnValue.substring(0, strReturnValue.length() -
1
)
;
}/* No
(more) trailing file-separators found; end loop. */
else
break
;
}/*
* Start
the process to identify and remove the part with the file-name or the
* last directory-name.
* Search for the
last position of the file-separator within the string. */
int
intFileSeparatorPosition
= strReturnValue.lastIndexOf(strFileSeparator);
/* See
if there was one found. */
if
(intFileSeparatorPosition
> 0) {
/* File-Separator
found; remove the aub-string after it.
* The removal
includes the found file-separator. */
strReturnValue
= strReturnValue.substring(0,
intFileSeparatorPosition
)
;
}/* String
contains only a File-Name or only one hierarchy of a directory-name
or
* does not even specify a File- or Directory-Name:
return empty String. */
else
strReturnValue
=
""
;
/*
* Return
the String with the remaining directory-string. */
return
strReturnValue;
}/*
*
--------------------
* Method to remove one or more
File-Separator-characters at the end of the passed String.
*/
public
static
String
removeTrailingFileSeparator(String parmDirectoryAndFileName) {
/*
*
Check if the parameter is valid; otherwise return empty String.
*/
if
(parmDirectoryAndFileName
==
null
)
return
""
;
/*
*
Define a String for the Return-value and fill it with a trimmed
(without leading or trailing
* blanks) version of the
input-string. */
String
strReturnValue = parmDirectoryAndFileName.trim(
)
;
/*
*
Get the system-specific file-separator; that is the character that
separates the
* directory-names (and the file-name from
the last directory) within the String with
* the
Directory- and File-Name. */
String
strFileSeparator =
System.getProperty(
"file.separator")
;
/*
* Inspect
for trailing 'file-separators' and remove them. */
for
(;;)
{
if
(strReturnValue.endsWith(strFileSeparator))
{
/* Take
the part of the string without the last character
* and
continue the for-loop to do inspecting. */
strReturnValue
= strReturnValue.substring(0, strReturnValue.length() -
1
)
;
}/* No
(more) trailing file-separators found; end loop. */
else
break
;
}/*
* Return
the String with the remaining directory-string. */
return
strReturnValue;
}/*
*
--------------------
* Method to add one
File-Separator-characters at the end of the passed String
* if
the string is not already ending with a File-Separator-character.
*/
public
static
String
addTrailingFileSeparator(String parmDirectoryAndFileName) {
/*
*
Check if the parameter is valid; otherwise return empty String.
*/
if
(parmDirectoryAndFileName
==
null
)
return
""
;
/*
*
Define a String for the Return-value and fill it with a trimmed
(without leading or trailing
* blanks) version of the
input-string. */
String
strReturnValue = parmDirectoryAndFileName.trim(
)
;
/*
*
Get the system-specific file-separator; that is the character that
separates the
* directory-names (and the file-name from
the last directory) within the String with
* the
Directory- and File-Name. */
String
strFileSeparator =
System.getProperty(
"file.separator")
;
/*
* Inspect
if a trailing 'file-separators' is alredy present. */
if
(strReturnValue.endsWith(strFileSeparator))
/* Already
ending with a File-Separator; return the trimmed Input-String.
*/
return
strReturnValue;
/* No
trailing file-separators found; add it at the end of the trimmed
Input-String. */
else
return
strReturnValue
+ strFileSeparator;
}/*
*
--------------------
* Method to check if the
directory-string passed as parameter is a valid Directory.
*/
public
static boolean
isDirectoryExisting(String
parmDirectoryAndFileName) {
/*
*
Check if the parameter is valid; otherwise return empty String.
*/
if
(parmDirectoryAndFileName
==
null
)
return
false
;
/*
*
Construct (respectively try to construct) a file which can be checked
later. */
File
file =
new
File(parmDirectoryAndFileName.trim(
))
;
/*
*
Verify if the String passed as parameter allowed to construct a File.
*/
if
(!
file.exists())
return
false
;
/*
*
Verify if the constructed File is a directory. */
if
(!
file.isDirectory())
return
false
;
/*
*
Passed parameter-String specified a valid Directory; return that.
*/
return
true
;
}/*
*
--------------------
* Method to check if the
directory-string passed as parameter is a valid File. */
public
static boolean
isFileExisting(String
parmDirectoryAndFileName) {
/*
*
Check if the parameter is valid; otherwise return empty String.
*/
if
(parmDirectoryAndFileName
==
null
)
return
false
;
/*
*
Construct (respectively try to construct) a file which can be checked
later. */
File
file =
new
File(parmDirectoryAndFileName.trim(
))
;
/*
*
Verify if the String passed as parameter allowed to construct a File.
*/
if
(!
file.exists())
return
false
;
/*
*
Verify if the constructed File is a File. */
if
(!
file.isFile())
return
false
;
/*
*
Passed parameter-String specified a valid File; return that.
*/
return
true
;
}
}
xxx
Dokument |
Inhalt |
Dieser
Leitfaden enthält die notwendigen Tätigkeiten für
die Entwicklung eines StartFrame (auch als Command-Center
bekannt). |